自學(xué)c++遇到的錯(cuò)誤,真是千奇百怪
報(bào)錯(cuò):
第一個(gè)
[Error]‘friend’ used outside of class
這個(gè)時(shí)候報(bào)錯(cuò)是因?yàn)樵陬愅舛x友元函數(shù)的時(shí)候,在函數(shù)頭部加了friend。
第二個(gè)
cannot have cv-qualifier//不能有CV限定
非成員函數(shù)不能有CV限定,友元函數(shù)不是類的成員函數(shù)聲明友元函數(shù)不能用const限定。
第三個(gè)報(bào)錯(cuò)比較多,但綜合看就一個(gè)最主要的
error: ‘time’ does not name a type
main.cpp: In function ‘int main()’:
main.cpp:8:13: error: expected ‘;’ before ‘a(chǎn)’
? ? 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(chǎn)’ 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(chǎn)’, 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(chǎn)’, which is of non-class type ‘const int’
? ?38 |? ? ? ? ?os<<"hours is "<<a.hour<<",minutes is "<<a.minutes<<"."<<endl;
? ? ? |? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^~~~~~~
最終發(fā)現(xiàn)原來是類名用了time,個(gè)人感覺還是構(gòu)造函數(shù)和time()函數(shù)重復(fù)了,最終更換類名終于通過了