我正在用C ++编写一个小程序。有一节课。你能在课堂上使用cout来输出数据。例如cout<< “美好时光”; 那么你不需要重载<<运营商? 我试图这样做,但它不起作用只是一些错误。如何通过cout在课堂上输出数据 CPP文件:
#include "University.hpp"
#include "Course.hpp"
//#include "Lecture.hpp"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
University::University() {
}
University::University(int capacity, string uni) {
university = uni;
cap = capacity;
length = 0;
myCourse = new Course[cap];
}
University::University(University& orig) {
copy(orig);
}
University::~University() {
delete [] myCourse;
}
University & University:: operator=(University & other){
if(this != &other){
delete [] myCourse;
copy(other);
}
return *this;
}
void University:: copy(University & other){
if(this != &other){
length = other.length;
cap = other.cap;
myCourse = new Course[cap];
for(int x = 0; x < length; x++){
myCourse[x] = other.myCourse[x];
}
}
}
void University:: addCourse(Course syllabus){
if(length == cap){
cap *= 2;
Course * temp = new Course[cap];
for(int x = 0; x < length; x++){
temp[x] = myCourse[x];
}
myCourse = temp;
}
myCourse[length] = syllabus;
length++;
}
void University::listAll(){
for(int x = 0; x < length; x++){
cout << toString();// toString();
}
}
friend ostream& operator<<(ostream& os, const University& uni){
os <<uni.university <<uni.myCourse;
return os;
}
string University::toString(){
ostringstream ans;
for(int x = 0; x < length; x++){
ans << "The University " << university << " teaches " << myCourse[x] << myCourse[x].toString() << endl;
}
return ans.str();
}
部首:
#include <string>
#include "Course.hpp"
using namespace std;
#ifndef UNIVERSITY_HPP
#define UNIVERSITY_HPP
class University {
friend ostream& operator<< (ostream& os, const University& uni);
public:
University();
University(int capacity, string UoL);
University(University& orig);
~University();
void copy(University & other);
University & operator=(University & other);
void addCourse(Course syllabus);
void listAll();
string toString();
private:
Course * myCourse;
int length;
int cap;
string university;
};
#endif /* UNIVERSITY_HPP */
错误:
g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/University.o.d -o build/Debug/Cygwin-Windows/University.o University.cpp
University.cpp:72: error: can't initialize friend function `operator<<'
University.cpp:72: error: friend declaration not in class definition
University.cpp: In member function `std::string University::toString()':
University.cpp:81: error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)(&ans))), ((const char*)"The University ")))), ((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(((std::string*)((University*)this)) + 12u)))))), ((const char*)" teaches ")) << *(((University*)this)->University::myCourse + (+(((unsigned int)x) * 16u)))'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:63: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:74: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:86: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:121: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:155: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:98: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:178: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:189: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:193: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:179: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:214: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:238: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:219: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:261: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:284: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:307: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
University.cpp:72: note: std::ostream& operator<<(std::ostream&, const University&)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:504: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:499: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:612: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:567: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:465: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:460: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:505: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:449: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]
make[2]: *** [build/Debug/Cygwin-Windows/University.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `/cygdrive/g/Aristotelis/C++/Assessment_2'
make[1]: Leaving directory `/cygdrive/g/Aristotelis/C++/Assessment_2'
BUILD FAILED (exit value 2, total time: 3s)
答案 0 :(得分:2)
您尝试使用ostringstream::operator<<
参数调用Course
。您还需要为operator<<
定义Course
。对不起我以前错过了。您还应该从friend
的定义中删除operator<<(ostream&, const University&)
,因为它只需要在类定义中。
答案 1 :(得分:1)
试试这个例子。
#include <iostream>
using namespace std;
class A
{
friend ostream& operator<<(ostream& os, const A& a);
int _member;
public:
A() : _member(0)
{ }
A(int member) : _member(member)
{ }
};
ostream& operator<<(ostream&os, const A& a)
{
os << a._member;
return os;
}
int main()
{
A a(5);
cout << a << endl;
return 0;
}
使用它并满足您的需求。
答案 2 :(得分:0)
University.cpp:72: error: can't initialize friend function `operator<<'
University.cpp:72: error: friend declaration not in class definition
University.cpp: In member function `std::string University::toString()':
所以,我们看一下在成员函数`std :: string University :: toString()'中找到:
ans << ... << myCourse[x] << ...;
你确定你有超载吗?
friend ostream& operator<< (ostream& os, const Course& uni);
这不是你在这里展示的任何代码。