阅读:1236回复:4
书上的一个例子,我编译总是不通过.请帮我分析.
下面是书上得一个例子,
#include <iostream.h> class Counter { public: Counter(); Counter(int initialValue); ~Counter(){}; int GetItsVal() const {return itsVal;}; void SetItsVal(int x) {itsVal = x;}; Counter Add(const Counter &); private: int itsVal; } Counter::Counter(int initialValue):itsVal(initialValue){} Counter::Counter():itsVal(0){} Counter Counter::Add(const Counter & rhs) { return Counter(itsVal + rhs.GetItsVal()); } int main() { Counter varOne(2), varTwo(4), varThree; varThree = varOne.Add(varTwo); cout<<"varOne: "<<varOne.GetItsVal()<<endl; cout<<"varTwo: "<<varTwo.GetItsVal()<<endl; cout<<"varThree: "<<varThree.GetItsVal()<<endl; return 0; } 我编译不能通过,出现如下错误信息: D:\C++\Add\Add.cpp(17) : error C2533: 'Counter::Counter' : constructors not allowed a return type D:\C++\Add\Add.cpp(23) : error C2264: 'Counter::Counter' : error in function definition or declaration; function not called D:\C++\Add\Add.cpp(23) : error C2264: 'Counter::Counter' : error in function definition or declaration; function not called D:\C++\Add\Add.cpp(28) : error C2264: 'Counter::Counter' : error in function definition or declaration; function not called D:\C++\Add\Add.cpp(28) : error C2264: 'Counter::Counter' : error in function definition or declaration; function not called Error executing cl.exe. 请高手帮我分析这段程序的问题.谢谢! |
|
沙发#
发布于:2004-10-27 13:15
让大家见笑了.这个错误很幼稚.函数声明的{}后可以不用分号,类的声明{}后必须用分号.改正后就没事了.
|
|
板凳#
发布于:2004-10-27 14:27
偶尔语法错误,谁都会犯的
|
|
|
地板#
发布于:2004-10-28 09:10
尽管问题已经解决,但我还是那些关心我的陌生朋友.
|
|
地下室#
发布于:2004-10-28 09:11
糟了,我本打算放分,突然发现已经不能.不好意思.下次吧.
|
|