标题:[求助]程序错误提示不懂
只看楼主
taijixing
Rank: 1
等 级:新手上路
威 望:1
帖 子:118
专家分:0
注 册:2007-6-9
 问题点数:0 回复次数:2 
[求助]程序错误提示不懂

#include <iostream>
static int n=0;
class CType
{
public:
CType()
{
m_n=n++;
int m_n;
}
};

CType d;
void fs()
{
static CType s;
cout<<"s."<<s.m_n<<endl;
}

void fa()
{
CType a;
cout<<"a."<<a.m_n<<endl;
}

void main()
{
fs();
fs();
fa();
fa();
}


--------------------Configuration: diaoyong2 - Win32 Debug--------------------
Compiling...
diaoyong2.cpp
e:\vc++\add\diaoyong2\diaoyong2.cpp(8) : error C2065: 'm_n' : undeclared identifier
e:\vc++\add\diaoyong2\diaoyong2.cpp(17) : error C2065: 'cout' : undeclared identifier
e:\vc++\add\diaoyong2\diaoyong2.cpp(17) : error C2297: '<<' : illegal, right operand has type 'char [3]'
e:\vc++\add\diaoyong2\diaoyong2.cpp(17) : error C2039: 'm_n' : is not a member of 'CType'
e:\vc++\add\diaoyong2\diaoyong2.cpp(4) : see declaration of 'CType'
e:\vc++\add\diaoyong2\diaoyong2.cpp(17) : error C2065: 'endl' : undeclared identifier
e:\vc++\add\diaoyong2\diaoyong2.cpp(23) : error C2297: '<<' : illegal, right operand has type 'char [3]'
e:\vc++\add\diaoyong2\diaoyong2.cpp(23) : error C2039: 'm_n' : is not a member of 'CType'
e:\vc++\add\diaoyong2\diaoyong2.cpp(4) : see declaration of 'CType'
执行 cl.exe 时出错.

diaoyong2.exe - 1 error(s), 0 warning(s)

搜索更多相关主题的帖子: 程序错误 提示 
2007-07-26 23:36
IPV6
Rank: 1
等 级:新手上路
威 望:2
帖 子:265
专家分:0
注 册:2006-9-7
得分:0 

我先指出你的错误,然后给你三个修改过的程序。
你看看,是不是这样才对。
-----------------------------------------------
#include <iostream> //引用头文件应为iastream.h这是标准。
static int n=0;
class CType
{
public:
CType()
{
m_n=n++;
int m_n; //它不是CType的数据成员,它的定义应在引用之前
}
};

CType d;
void fs()
{
static CType s;//静态类不可以被多次定义
cout<<"s."<<s.m_n<<endl;//在这里不能引用类中不存在的数据成员
}

void fa()
{
CType a;
cout<<"a."<<a.m_n<<endl;//这里犯了一样的错误
}

void main()
{
fs();
fs();
fa();
fa();
}

----------------------------------------------------
编译正确示例一
#include <iostream.h>
static int n=0;
class CType
{
private:
int m_n;
public:
CType()
{
m_n=n++;
cout<<m_n<<endl;
}
};

CType d;
void fs()
{
cout<<"s.";
CType s;
}

void fa()
{
cout<<"a.";
CType a;
}

void main()
{
fs();
fs();
fa();
fa();
}
--------------------------------------------
编译正确示例二
#include <iostream.h>
static int n=0;
class CType
{
public:
CType()
{
int m_n;
m_n=n++;
cout<<m_n<<endl;
}
};

CType d;
void fs()
{
cout<<"s.";
CType s;
}

void fa()
{
cout<<"a.";
CType a;
}

void main()
{
fs();
fs();
fa();
fa();
}
----------------------------------------------------
编译正确示例三
#include <iostream.h>
static int n=0;
class CType
{
public:

CType()
{
m_n=n++;
}
int m_n;
};

CType d;
void fs()
{
CType s;
cout<<"s."<<s.m_n<<endl;

}

void fa()
{
CType a;
cout<<"a."<<a.m_n<<endl;
}

void main()
{
fs();
fs();
fa();
fa();
}

[此贴子已经被作者于2007-7-27 8:46:16编辑过]


2007-07-27 08:12
taijixing
Rank: 1
等 级:新手上路
威 望:1
帖 子:118
专家分:0
注 册:2007-6-9
得分:0 
多谢楼上大哥
2007-07-27 13:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-158424-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.241369 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved