读书人

又来麻烦大家了:帮小弟我看几行代码吧

发布时间: 2012-04-15 18:39:21 作者: rapoo

又来麻烦大家了:帮我看几行代码吧,应该很简单的
[code=C/C++][/code]#include <iostream>
using namespace std;
class anyone
{
public:
anyone(){};
void setweight(int i)
{anyone * pp;
pp->weight=i;
cout <<pp->weight<<endl;
};
protected:
int weight;
anyone* Pfirst;
}
/* anyone::setweight(int i)
{anyone * pp;
pp->weight=i;
cout <<pp->weight<<endl;
}*/

int main()
{
class first;
first.setweight(10);
return 1;
}


运行报错:
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
2.cpp
G:\MyProjects\C程序设计教程3\2.cpp(22) : error C2628: 'anyone' followed by 'int' is illegal (did you forget a ';'?)
G:\MyProjects\C程序设计教程3\2.cpp(25) : error C2143: syntax error : missing ';' before '.'
G:\MyProjects\C程序设计教程3\2.cpp(25) : error C2143: syntax error : missing ';' before '.'
G:\MyProjects\C程序设计教程3\2.cpp(26) : error C2664: '__thiscall anyone::anyone(const class anyone &)' : cannot convert parameter 1 from 'const int' to 'const class anyone &'
Reason: cannot convert from 'const int' to 'const class anyone'
No constructor could take the source type, or constructor overload resolution was ambiguous
G:\MyProjects\C程序设计教程3\2.cpp(26) : error C2553: no legal conversion of return value to return type 'class anyone *'
G:\MyProjects\C程序设计教程3\2.cpp(27) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

2.obj - 1 error(s), 0 warning(s)

麻烦大家帮我看看什么问题?感激不尽

[解决办法]

C/C++ code
#include <iostream> using namespace std; class anyone { public:     anyone(){};     void setweight(int i)     { anyone * pp;     pp->weight=i;     cout <<pp->weight <<endl;     }; protected:     int weight;     anyone* Pfirst; } ;//少了;/* anyone::setweight(int i) { anyone * pp; pp->weight=i; cout <<pp->weight <<endl; }*/ int main() {     class anyone first;  //少了类型名    first.setweight(10);     return 1; }
[解决办法]
C/C++ code
#include <iostream> using namespace std; class anyone { public:     anyone(){};     void setweight(int i)     { anyone *pp=new anyone;//修改     pp->weight=i;     cout<<pp->weight<<endl;    delete pp;    }; protected:     int weight;     anyone* Pfirst; }; ;//修改/* anyone::setweight(int i) { anyone * pp; pp->weight=i; cout <<pp->weight <<endl; }*/ int main() {     //class first;     anyone first;;//修改    first.setweight(10);     return 0; } 

读书人网 >C++

热点推荐