我对超视镜+有问题。
这是我的代码:
class Odcinek : public Figura
{
public:
Odcinek(int ile,string imie,char znak): Figura(ile,imie,znak){}
friend Odcinek& operator+(int liczba);
virtual void wypisz() const;
};
Odcinek& operator+(int liczba);
{
Odcinek Od;
Od.ile = Od.ile+liczba;
Od.imie = imie;
Od.znak = znak;
return Od;
}
我希望它能工作:
Odcinek F3(5,"Odcinek drugi",'*');
F3+3;
||=== Build: Debug in Figura (compiler: GNU GCC Compiler) ===| include\Figura.h|29|error: 'Odcinek& operator+(int)' must have an argument of class or enumerated type| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp||In function 'int main()':| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|error: no match for 'operator+' (operand types are 'Odcinek' and 'int')| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h|341|note: candidate: template std::reverse_iterator std::operator+(typename std::reverse_iterator::difference_type, const std::reverse_iterator&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h|341|note: template argument deduction/substitution failed:| include\Figura.h|29|error: 'Odcinek& operator+(int)' must have an argument of class or enumerated type| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\src\Figura.cpp|67|error: 'Odcinek& operator+(int)' must have an argument of class or enumerated type| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\src\Figura.cpp|68|error: expected unqualified-id before '{' token| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: mismatched types 'const std::reverse_iterator' and 'int'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h|1200|note: candidate: template std::move_iterator std::operator+(typename std::move_iterator::difference_type, const std::move_iterator&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_iterator.h|1200|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: mismatched types 'const std::move_iterator' and 'int'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|4929|note: candidate: template std::__cxx11::basic_string std::operator+(const std::__cxx11::basic_string&, const std::__cxx11::basic_string&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|4929|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: 'Odcinek' is not derived from 'const std::__cxx11::basic_string'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.tcc|1147|note: candidate: template std::__cxx11::basic_string std::operator+(const _CharT*, const std::__cxx11::basic_string&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.tcc|1147|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: mismatched types 'const _CharT*' and 'Odcinek'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.tcc|1163|note: candidate: template std::__cxx11::basic_string std::operator+(_CharT, const std::__cxx11::basic_string&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.tcc|1163|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: mismatched types 'const std::__cxx11::basic_string' and 'int'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|4966|note: candidate: template std::__cxx11::basic_string std::operator+(const std::__cxx11::basic_string&, const _CharT*)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|4966|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: 'Odcinek' is not derived from 'const std::__cxx11::basic_string'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|4982|note: candidate: template std::__cxx11::basic_string std::operator+(const std::__cxx11::basic_string&, _CharT)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|4982|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: 'Odcinek' is not derived from 'const std::__cxx11::basic_string'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|4994|note: candidate: template std::__cxx11::basic_string std::operator+(std::__cxx11::basic_string&&, const std::__cxx11::basic_string&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|4994|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: 'Odcinek' is not derived from 'std::__cxx11::basic_string'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5000|note: candidate: template std::__cxx11::basic_string std::operator+(const std::__cxx11::basic_string&, std::__cxx11::basic_string&&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5000|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: 'Odcinek' is not derived from 'const std::__cxx11::basic_string'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5006|note: candidate: template std::__cxx11::basic_string std::operator+(std::__cxx11::basic_string&&, std::__cxx11::basic_string&&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5006|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: 'Odcinek' is not derived from 'std::__cxx11::basic_string'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5018|note: candidate: template std::__cxx11::basic_string std::operator+(const _CharT*, std::__cxx11::basic_string&&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5018|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: mismatched types 'const _CharT*' and 'Odcinek'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5024|note: candidate: template std::__cxx11::basic_string std::operator+(_CharT, std::__cxx11::basic_string&&)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5024|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: mismatched types 'std::__cxx11::basic_string' and 'int'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5030|note: candidate: template std::__cxx11::basic_string std::operator+(std::__cxx11::basic_string&&, const _CharT*)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5030|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: 'Odcinek' is not derived from 'std::__cxx11::basic_string'| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5036|note: candidate: template std::__cxx11::basic_string std::operator+(std::__cxx11::basic_string&&, _CharT)| c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h|5036|note: template argument deduction/substitution failed:| C:\Users\mkkar\Desktop\Programowanie obiektowe ~kolokwium\Figura\main.cpp|15|note: 'Odcinek' is not derived from 'std::__cxx11::basic_string'| ||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
答案 0 :(得分:0)
您的代码中有很多问题
1)您的运算符定义中还有一个分号
Odcinek& operator+(int liczba);
^
2)您的operator+
仅具有一个参数,但是您的代码使用了两个参数。我认为解决方案是不要使其成为朋友函数。或者,您可以将其保留为好友,但添加第二个参数。但是,这将需要对已编写的代码进行更多更改。
3)您对operator+
的实现似乎使用了未初始化的值
Odcinek Od;
Od.ile = Od.ile+liczba;
我猜应该是
Odcinek Od;
Od.ile = ile+liczba;
4)最后,最重要的是,您的operator+
返回对不再存在的对象的引用
Odcinek& operator+(int liczba)
{
Odcinek Od; // Od created here
Od.ile = ile+liczba;
Od.imie = imie;
Od.znak = znak;
return Od;
} // Od destroyed here
但是您返回对已经销毁的Od
对象的引用。答案是返回一个值,而不是引用。当您按值返回Od
对象的副本并返回该副本时,Od
被销毁并不重要。
这是一个更好的版本,它是否有效取决于您尚未向我们展示的代码,但请尝试一下。
class Odcinek : public Figura
{
public:
Odcinek(int ile,string imie,char znak): Figura(ile,imie,znak){}
Odcinek operator+(int liczba) const;
virtual void wypisz() const;
};
Odcinek Odcinek::operator+(int liczba) const
{
Odcinek Od;
Od.ile = ile+liczba;
Od.imie = imie;
Od.znak = znak;
return Od;
}