我正在为我的课程编写代码并遇到我无法处理的错误。错误如下。此外,我不允许更改主文件中的任何内容:
In file included from lab13.cpp:15:0:
lab13.h: In member function ‘TSeries& TSeries::operator()(int, int)’:
lab13.h:10:39: warning: no return statement in function returning non-void [-Wreturn-type]
TSeries &operator()(int, int){}
^
g++ -c -Wall lab13f.cpp
In file included from lab13f.cpp:1:0:
lab13.h:15:10: error: ‘ostream’ in namespace ‘std’ does not name a type
friend std::ostream &operator<<(std::ostream &o,const TSeries &ts);
^
lab13.h: In member function ‘TSeries& TSeries::operator()(int, int)’:
lab13.h:10:39: warning: no return statement in function returning non-void [-Wreturn-type]
TSeries &operator()(int, int){}
^
lab13f.cpp: At global scope:
lab13f.cpp:13:9: error: prototype for ‘TSeries TSeries::operator()(int, int)’ does not match any in class ‘TSeries’
TSeries TSeries::operator()(int, int){}
^
In file included from lab13f.cpp:1:0:
lab13.h:10:18: error: candidate is: TSeries& TSeries::operator()(int, int)
TSeries &operator()(int, int){}
^
lab13f.cpp:16:9: error: prototype for ‘TSeries TSeries::operator+(TSeries&)’ does not match any in class ‘TSeries’
TSeries TSeries::operator+(TSeries &ts){
^
In file included from lab13f.cpp:1:0:
lab13.h:9:17: error: candidate is: const TSeries TSeries::operator+(const TSeries&) const
const TSeries operator+(const TSeries &ts)const;
^
lab13f.cpp:45:19: error: definition of implicitly-declared ‘TSeries::~TSeries()’
TSeries::~TSeries(){}
^
lab13f.cpp: In member function ‘TSeries& TSeries::operator=(TSeries (*)(int, int))’:
lab13f.cpp:47:52: warning: no return statement in function returning non-void [-Wreturn-type]
TSeries &TSeries::operator=(TSeries(int a, int b)){}
^
lab13f.cpp: In function ‘std::ostream& operator<<(std::ostream&, const TSeries&)’:
lab13f.cpp:51:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
make: *** [lab13f.o] Błąd 1
我花了几天时间试图解决这个问题并最终放弃了。 Hopefull有人会帮助我解决我的斗争:)我非常感激。
我已经忘记补充说主要文件是由导师写的,并且在没有任何情况下我不允许更改其中的任何内容。
答案 0 :(得分:1)
也许,这是一个错字。这样:
TSeries series4=series1(2,4);
不会使用构造函数参数series4
和2
创建4
,而是尝试调用series1
&#39; s(注意:这是变量) operator()(int, int)
。并且没有定义这样的运营商。
我想你需要
TSeries series4=TSeries(2,4);
甚至更好:
TSeries series4(2,4);
此外,这样的结构:
series1+=1.,0.,3.;
实际上只会拨打operator +=
一次。要清楚,因为我不确定你是否期望这样。您可以在C ++中阅读operator,
所以,如果你想要3个补充,你需要:
series1+=1.;
series1+=0.;
series1+=3.;
另请注意@ RSahu关于operator=
的答案,我赞成(很好听,我没有注意到)。
答案 1 :(得分:1)
该行
TSeries &operator=(TSeries(int a, int b));
声明一个函数,其参数是一个函数,它有两个类型为int
的输入,返回类型为TSeries
。
我认为你并不打算这样做。
将使用以下语法声明复制赋值运算符:
TSeries &operator=(TSeries const& rhs);
此外,目前还不清楚你的意思:
TSeries series4=series1(2,4);
也许,你打算使用:
// Will use the compiler generated copy constructor
// since you haven't declared one.
TSeries series4=series1;
或
// Will use the constructor that takes two ints
TSeries series4(2,4);
答案 2 :(得分:0)
编译器抱怨,因为series1(2,4)调用缺少的TSeries :: operator()(int,int),而不是构造函数TSeries(int,int)