自学c++遇到的错误,真是千奇百怪
报错:
第一个
[Error]‘friend’ used outside of class
这个时候报错是因为在类外定义友元函数的时候,在函数头部加了friend。
第二个
cannot have cv-qualifier//不能有CV限定
非成员函数不能有CV限定,友元函数不是类的成员函数声明友元函数不能用const限定。
第三个报错比较多,但综合看就一个最主要的
error: ‘time’ does not name a type
main.cpp: In function ‘int main()’:
main.cpp:8:13: error: expected ‘;’ before ‘a’
8 | time a(3,35);
| ^~
| ;
main.cpp:9:13: error: expected ‘;’ before ‘b’
9 | time b(2,48);
| ^~
| ;
main.cpp:10:13: error: expected ‘;’ before ‘t’
10 | time t;
| ^~
| ;
main.cpp:11:21: error: ‘a’ was not declared in this scope
11 | cout<<"a="<<a<<"\tb="<<b<<endl;
| ^
main.cpp:11:32: error: ‘b’ was not declared in this scope
11 | cout<<"a="<<a<<"\tb="<<b<<endl;
| ^
main.cpp:12:9: error: ‘t’ was not declared in this scope; did you mean ‘tm’?
12 | t=a+b;
| ^
| tm
socure.cpp:7:1: error: ‘time’ does not name a type
7 | time& operator+(time& a,const time& b)
| ^~~~
socure.cpp:15:1: error: ‘time’ does not name a type
15 | time& operator-(time&a,const time&b)
| ^~~~
socure.cpp:23:1: error: ‘time’ does not name a type
23 | time& operator*(time& a,const double d)
| ^~~~
socure.cpp:36:40: error: ‘time’ does not name a type
36 | ostream& operator<<(ostream & os,const time& a)
| ^~~~
socure.cpp: In function ‘std::ostream& operator<<(std::ostream&, const int&)’:
socure.cpp:38:28: error: request for member ‘hour’ in ‘a’, which is of non-class type ‘const int’
38 | os<<"hours is "<<a.hour<<",minutes is "<<a.minutes<<"."<<endl;
| ^~~~
socure.cpp:38:52: error: request for member ‘minutes’ in ‘a’, which is of non-class type ‘const int’
38 | os<<"hours is "<<a.hour<<",minutes is "<<a.minutes<<"."<<endl;
| ^~~~~~~
最终发现原来是类名用了time,个人感觉还是构造函数和time()函数重复了,最终更换类名终于通过了