在VS 2012上编译,但不使用Netbeans/MinGW-重载<<



我正在编写的程序有问题。我和visualstudio一起写这个项目,因为我发现它更容易处理,但我需要把它作为一个netbeans项目交上来。

我有以下问题:

我在等待鸵鸟<lt;操作员以特定格式输出图书对象,如您在下方所见

ostream &operator << (ostream &output, const Book &book) {
//output stream
output  << book.author
        << ", "" << book.title
        << "" (" << book.pages
        << " pp.)";
//returns the output stream.
return output;
}

这段代码在我的visual studio项目中编译得很好,但在netbeans上我得到了以下错误:

"/C/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE=                    SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/My   Documents/NetBeansProjects/LibraryProjectCoursework'
"/C/msys/1.0/bin/make.exe"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-    Windows/libraryprojectcoursework.exe
make.exe[2]: Entering directory `/d/My Documents/NetBeansProjects/LibraryProjectCoursework'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/Book.o.d"
g++    -c -g -std=c++11 -MMD -MP -MF "build/Debug/MinGW-Windows/Book.o.d" -o     build/Debug/MinGW-Windows/Book.o Book.cpp
Book.cpp: In function 'std::ostream& operator<<(std::ostream&, const  Book&)':
Book.cpp:50:13: error: no match for 'operator<<' (operand types are   'std::basic_ostream<char>' and 'const char [4]')
         << ", "" << book.title
         ^
Book.cpp:50:13: note: candidates are:
Book.cpp:47:10: note: std::ostream& operator<<(std::ostream&, const Book&)
ostream &operator << (ostream &output, const Book &book) {
      ^
Book.cpp:47:10: note:   no known conversion for argument 2 from 'const char   [4]' to 'const Book&'
In file included from    c:mingwlibgccmingw324.8.1includec++string:52:0,
             from Book.h:3,
             from Book.cpp:1:
c:mingwlibgccmingw324.8.1includec++bitsbasic_string.h:2753:5: note:   template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT,   _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const   std::basic_string<_CharT, _Traits, _Alloc>&)
 operator<<(basic_ostream<_CharT, _Traits>& __os,
 ^ 
c:mingwlibgccmingw324.8.1includec++bitsbasic_string.h:2753:5: note:      template argument deduction/substitution failed:
Book.cpp:50:16: note:   mismatched types 'const std::basic_string<_CharT,  _Traits, _Alloc>' and 'const char [4]'
         << ", "" << book.title
            ^
make.exe[2]: *** [build/Debug/MinGW-Windows/Book.o] Error 1
make.exe[2]: Leaving directory `/d/My     Documents/NetBeansProjects/LibraryProjectCoursework'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/d/My    Documents/NetBeansProjects/LibraryProjectCoursework'
make.exe": *** [.build-impl] Error 2MSYS-1.0.10 Build:2004-03-15 07:17
Exception: STATUS_ACCESS_VIOLATION at eip=00000000
eax=00000000 ebx=60EA31E4 ecx=00301E9E edx=60EA31E4 esi=00000000  edi=60EA00D4
ebp=0028FD40 esp=0028FD14 program=c:msys1.0binmkdir.exe
cs=0023 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame     Function  Args
End of stack trace

任何帮助都将不胜感激!

它似乎在抱怨没有std::ostream<char>char[]的重载定义,所以问题可能是out << "string literal"。这听起来不像是你能解决的问题,所以你必须更改代码来删除字符串,或者以某种方式将字符串分配给一个变量,该变量确实有重载,比如可能将其包装在std::string中。

编辑:哇,真不敢相信我没有想到including定义的过载。SMH。

相关内容

最新更新