C++ error constructor parameter - c++

i'm playing around with inheritance. I'm completely baffled by the compiler
error i'm getting and looking it up it seems completely unrelated to what i'm trying
to do which is simply initialize my class.
here are the errors:
In file included from main.cpp:2:0:
student.h: In constructor ‘Student::Student(std::string, int, std::string, double)’:
student.h:13:3: error: class ‘Student’ does not have any field named ‘gnu_dev_major’
student.h: In function ‘std::ostream& operator<<(std::ostream&, const Student&)’:
student.h:25:8: error: no match for ‘operator<<’ in ‘os << "Name\011: "’
student.h:25:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [8]’ to ‘const Student&’
/usr/include/c++/4.6/bits/basic_string.h:2693: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>&)
student.h:25:32: error: ‘endl’ is not a member of ‘std’
student.h:26:8: error: no match for ‘operator<<’ in ‘os << "Age\011: "’
student.h:26:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [7]’ to ‘const Student&’
/usr/include/c++/4.6/bits/basic_string.h:2693: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>&)
student.h:26:30: error: ‘endl’ is not a member of ‘std’
student.h:27:8: error: no match for ‘operator<<’ in ‘os << "Major\011: "’
student.h:27:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [9]’ to ‘const Student&’
/usr/include/c++/4.6/bits/basic_string.h:2693: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>&)
student.h:27:34: error: ‘endl’ is not a member of ‘std’
student.h:28:8: error: no match for ‘operator<<’ in ‘os << "GPA\011: "’
student.h:28:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [7]’ to ‘const Student&’
/usr/include/c++/4.6/bits/basic_string.h:2693: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>&)
student.h:28:30: error: ‘endl’ is not a member of ‘std’
main.cpp: In function ‘int main()’:
main.cpp:8:2: error: ‘cout’ was not declared in this scope
whats confusing me is the very first error i'm getting:
student.h: In constructor ‘Student::Student(std::string, int,
std::string, double)’: student.h:13:3: error: class ‘Student’ does not
have any field named ‘gnu_dev_major’
here is my code:
1 #ifndef STUDENT_H
2 #define STUDENT_H
3
4 #include <iostream>
5 #include <string>
6 #include "person.h"
7
8 class Student : public Person {
9 friend std::ostream & operator<<(std::ostream &,const Student &);
10 public:
11 Student(std::string name, int age, std::string m="undecided", double gpa=0.0) :
12 Person::Person(name,age),
13 major(m),
14 gpa(gpa)
15 {}
16
17
18
19 protected:
20 double gpa;
21 std::string major;
22 };
23
24 std::ostream & operator<<(std::ostream &os,const Student &s){
25 os << "Name\t: " << s.name << std::endl;
26 os << "Age\t: " << s.age << std::endl;
27 os << "Major\t: " << s.major << std::endl;
28 os << "GPA\t: " << s.gpa << std::endl;
29 }
30
31 #endif
also if anyone has a pointer as to what might be going wrong in my overloaded
operator << help with that is also appreciated =).

You need to declare your overloaded << operator as friend inside your class:
Inside your Student class declare it as follows:
class Student {
// rest of code
friend std::ostream & operator<<(std::ostream &os,const Student &s);
}

Related

header file doesn't seem to be recognizing variables from main function

First of all, I am aware it is not good practice to do anything other then declare your functions etc... in the header file, but my teacher wanted us to follow this format so I am.
The goal of this particular function is to store user input into the various arrays in 'int main'.
The issue im getting with this function, as well as some others is that the compiler is throwing various errors I think are related to it not being able to recognize the arrays from 'int main'. (Just my guess).
Disclaimer: I have been working on this program since 4 pm till the time of posting this at 2 am so if im doing something really stupid I apologize, im honestly just going through the motions at this point.
//header file
void getVisitorInfo(int* age[], double* hours[], string* country[]) {
cout<<"Enter your country: ";
getline(cin, country[vCount]);
cout<<endl;
cout<<"Enter your age: ";
cin>>age[vCount];
cout<<endl;
cout<<"Enter the number of hours you explored: ";
cin>>hours[vCount];
cout<<endl;
vCount++;
}
//main file
const int max = 100;
int main() {
int age[max], vOldest, vYoungest, vAverage, userChoice;
double hours[max], vShortest, vLongest, vAverage;
string country[max];
bool pass;
//etc...
}
//a few of many errors
test.cpp: In function ‘void getVisitorInfo(int**, double**, std::string**)’:
test.cpp:84:31: error: no matching function for call to ‘getline(std::istream&, std::string*&)’
getline(cin, country[vCount]);
^
test.cpp:84:31: note: candidates are:
In file included from /usr/include/c++/4.8.2/string:53:0,
from /usr/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/include/c++/4.8.2/ios:42,
from /usr/include/c++/4.8.2/ostream:38,
from /usr/include/c++/4.8.2/iostream:39,
from test.cpp:1:
/usr/include/c++/4.8.2/bits/basic_string.tcc:1068:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
getline(basic_istream<_CharT, _Traits>& __in,
^
/usr/include/c++/4.8.2/bits/basic_string.tcc:1068:5: note: template argument deduction/substitution failed:
test.cpp:84:31: note: mismatched types ‘std::basic_string<_CharT, _Traits, _Alloc>’ and ‘std::string* {aka std::basic_string<char>*}’
getline(cin, country[vCount]);
^
In file included from /usr/include/c++/4.8.2/string:52:0,
from /usr/include/c++/4.8.2/bits/locale_classes.h:40,
from /usr/include/c++/4.8.2/bits/ios_base.h:41,
from /usr/include/c++/4.8.2/ios:42,
from /usr/include/c++/4.8.2/ostream:38,
from /usr/include/c++/4.8.2/iostream:39,
from test.cpp:1:
/usr/include/c++/4.8.2/bits/basic_string.h:2793:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
getline(basic_istream<_CharT, _Traits>& __is,
^
/usr/include/c++/4.8.2/bits/basic_string.h:2793:5: note: template argument deduction/substitution failed:
test.cpp:84:31: note: mismatched types ‘std::basic_string<_CharT, _Traits, _Alloc>’ and ‘std::string* {aka std::basic_string<char>*}’
getline(cin, country[vCount]);
^
test.cpp:89:6: error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘int*’)
cin>>age[vCount];
^

Unable to use ostream << overload with stringstream no known conversion for argument 1

I'm when trying to write to a stringstream using a overload of << for ostream gcc fails with:
error: no match for 'operator<<' (operand types are 'std::stringstream&() {aka std::__cxx11::basic_stringstream&()}' and 'const Test')
As far as I can tell the operator is defined, I've used similar code with stringstream (input operator for other classes where I overload for istream) with out any problems using the same compiler.
Example Code:
// Header
#include <istream>
#include <ostream>
class Test {
public:
Test* Clone() const { return onClone(); }
friend std::istream& operator >> (std::istream& lhs, Test& rhs) { rhs.onLoad(lhs); return lhs; }
friend std::ostream& operator << (std::ostream& lhs, const Test& rhs) { rhs.onSave(lhs); return lhs; }
private:
virtual void onLoad(std::istream& in) {}
virtual void onSave(std::ostream& out) const {}
Test* Test::onClone() const;
};
// Source
#include <sstream>
Test* Test::onClone() const
{
Test * tmp = new Test();
std::stringstream& buf();
buf << (*this); //!< ERROR HERE
buf >> (*tmp);
return tmp;
}
Filtered Build Output:
g++.exe -Wzero-as-null-pointer-constant -std=c++14 -m64 -Wcast-align -Wundef -Wfloat-equal -Winline -Wunreachable-code -Wswitch-enum -Wswitch-default -Weffc++ -Wzero-as-null-pointer-constant -pg -g -D_WIN32 -D_UNICODE -Iinclude -c test.cpp -o obj\Debug\test.o
test.cpp: In member function 'Test* Test::onClone() const':
test.cpp:23:9: error: no match for 'operator<<' (operand types are 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' and 'const Test')
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:628:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^
%LibaryPath%/ostream:628:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
%LibaryPath%/ostream:574:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
%LibaryPath%/ostream:569:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:556:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
%LibaryPath%/ostream:556:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/ostream:638:0,
from %LibaryPath%/istream:39,
from test.cpp:2:
%LibaryPath%/bits/ostream.tcc:321:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
%LibaryPath%/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:539:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
%LibaryPath%/ostream:539:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:519:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
%LibaryPath%/ostream:519:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:514:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
%LibaryPath%/ostream:514:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:508:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
%LibaryPath%/ostream:508:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:502:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
%LibaryPath%/ostream:502:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:497:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
%LibaryPath%/ostream:497:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/bits/ios_base.h:46:0,
from %LibaryPath%/ios:42,
from %LibaryPath%/istream:38,
from test.cpp:2:
%LibaryPath%/system_error:209:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
^
%LibaryPath%/system_error:209:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/string:52:0,
from %LibaryPath%/bits/locale_classes.h:40,
from %LibaryPath%/bits/ios_base.h:41,
from %LibaryPath%/ios:42,
from %LibaryPath%/istream:38,
from test.cpp:2:
%LibaryPath%/bits/basic_string.h:5167:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
%LibaryPath%/bits/basic_string.h:5167:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
test.cpp:9:26: note: candidate: std::ostream& operator<<(std::ostream&, const Test&)
friend std::ostream& operator << (std::ostream& lhs, const Test& rhs) { rhs.onSave(lhs); return lhs; }
^
test.cpp:9:26: note: no known conversion for argument 1 from 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' to 'std::ostream& {aka std::basic_ostream<char>&}'
test.cpp:24:9: error: no match for 'operator>>' (operand types are 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' and 'Test')
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:924:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&)
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
^
%LibaryPath%/istream:924:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:808:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^
%LibaryPath%/istream:808:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:803:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
^
%LibaryPath%/istream:803:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:761:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
^
%LibaryPath%/istream:761:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:756:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
^
%LibaryPath%/istream:756:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from %LibaryPath%/istream:934:0,
from test.cpp:2:
%LibaryPath%/bits/istream.tcc:923:5: note: candidate: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
^
%LibaryPath%/bits/istream.tcc:923:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from %LibaryPath%/istream:934:0,
from test.cpp:2:
%LibaryPath%/bits/istream.tcc:955:5: note: candidate: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
^
%LibaryPath%/bits/istream.tcc:955:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from %LibaryPath%/string:53:0,
from %LibaryPath%/bits/locale_classes.h:40,
from %LibaryPath%/bits/ios_base.h:41,
from %LibaryPath%/ios:42,
from %LibaryPath%/istream:38,
from test.cpp:2:
%LibaryPath%/bits/basic_string.tcc:1441:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator>>(basic_istream<_CharT, _Traits>& __in,
^
%LibaryPath%/bits/basic_string.tcc:1441:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
test.cpp:8:26: note: candidate: std::istream& operator>>(std::istream&, Test&)
friend std::istream& operator >> (std::istream& lhs, Test& rhs) { rhs.onLoad(lhs); return lhs; }
^
test.cpp:8:26: note: no known conversion for argument 1 from 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' to 'std::istream& {aka std::basic_istream<char>&}'
std::stringstream& buf();
buf << (*this); //!< ERROR HERE
Here buf is declared as a function returning a reference to a stringstream. If that was your intention, and you have defined that function elsewhere, you need to call the function to get the reference:
buf() << *this;
If you intended to have buf as a local stream, the declaration would be just
std::stringstream buf;
without the & and ().
The buf is a function declaration having no argument and return type as std::stringstream&. That's why no operator match error is shown by g++.
When creating a local object with default constructor, you should create it like,
std::stringstream buf;
It will remove no operator match errors.

std::ofstream not able to write std::string to file

Solved the problem already when checking a last time before posting this, but it looked somewhat evil to debug (at least for a newbie), so I'll post it anyway - feel free to delete.
The problem was that in the marked line below, ofstream seemed unable to write a simple string; the templating appeared to be the problem:
template <typename T>
void appendVectorToCSV(const std::string& header, std::vector<T> row,
const std::string& outfilename){
std::ofstream fout(outfilename);
fout << header;// << ","; /* The error line 80 */
...
This gives the error:
varUtils.hpp: In function ‘void appendVectorToCSV(std::string&, const std::vector<_RealType>&, const string&)’:
varUtils.hpp:80:10: error: no match for ‘operator<<’ (operand types are ‘std::ofstream {aka std::basic_ofstream<char>}’ and ‘std::string {aka std::basic_string<char>}’)
fout << header;// << ",";
^
varUtils.hpp:80:10: note: candidates are:
...
/usr/include/c++/4.8/complex:524:5: note: template<class _Tp, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::complex<_Tp>&)
operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
^
/usr/include/c++/4.8/complex:524:5: note: template argument deduction/substitution failed:
...
varUtils.hpp:80:13: note: ‘std::ofstream {aka std::basic_ofstream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’
fout << header;// << ",";
^
So, solution:
A missing header.
#include <iostream>
was already there, but not
#include <fstream>

Compiling C++ Program with a header file

Sorry I'm new to C++, but I am having a problem compiling a C++ program that uses the functions of a class defined in another file.
I have:
// GradeBook.h
// GradeBook class definition in a separate file from main.
//
#include <iostream>
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook (string name)
{
setCourseName(name);
} // end GradeBook constructor
void setCourseName(string name)
{
courseName = name;
}
string getCourseName()
{ return CourseName; }
void displayMessage()
{ cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
private:
string courseName;
}; //End of class
and this main file:
// grade.cpp
// Including class GradeBook from file GradeBook.h for use in main.
//
#include <iostream>
#include "Gradebook.h"
using namespace std;
int main()
{
GradeBook gradeBook1( "CS101 Introduction to C++ Programing");
GradeBook gradeBook2( "Cs102 Data Structures in C++");
cout <<"gradeBook1 create for course: "<<gradeBook1.getCourseName() <<"\ngradeBook2 created for course: "<<gradeBook2.getCourseName() <<endl;
}// end main
// grade.cpp
// Including class GradeBook from file GradeBook.h for use in main.
//
#include <iostream>
#include "Gradebook.h"
using namespace std;
int main()
{
GradeBook gradeBook1( "CS101 Introduction to C++ Programing");
GradeBook gradeBook2( "Cs102 Data Structures in C++");
cout <<"gradeBook1 create for course: "<<gradeBook1.getCourseName() <<"\ngradeBook2 created for course: "<<gradeBook2.getCourseName() <<endl;
}// end main
When I try to compile grade.cpp I get so many errors.
My operating system is Fedora, and I use g++ grade.cpp -o grade -Wall
to compile the program.
Can someone please help?
Error Message:
g++ grade.cpp -o grade -lm
In file included from grade.cpp:5:0:
Gradebook.h: In member function ‘std::string GradeBook::getCourseName()’:
Gradebook.h:23:11: error: ‘CourseName’ was not declared in this scope
{ return CourseName; }
^
Gradebook.h: In member function ‘void GradeBook::displayMessage()’:
Gradebook.h:26:66: error: no match for ‘operator<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’)
{ cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
^
Gradebook.h:26:66: note: candidates are:
In file included from /usr/include/c++/4.8.3/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8.3/bits/char_traits.h:39,
from /usr/include/c++/4.8.3/ios:40,
from /usr/include/c++/4.8.3/ostream:38,
from /usr/include/c++/4.8.3/iostream:39,
from grade.cpp:4:
/usr/include/c++/4.8.3/bits/stl_pair.h:220:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^
/usr/include/c++/4.8.3/bits/stl_pair.h:220:5: note: template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::pair<_T1, _T2>’
{ cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
^
In file included from /usr/include/c++/4.8.3/bits/stl_algobase.h:67:0,
from /usr/include/c++/4.8.3/bits/char_traits.h:39,
from /usr/include/c++/4.8.3/ios:40,
from /usr/include/c++/4.8.3/ostream:38,
from /usr/include/c++/4.8.3/iostream:39,
from grade.cpp:4:
/usr/include/c++/4.8.3/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator<(const reverse_iterator<_Iterator>& __x,
^
/usr/include/c++/4.8.3/bits/stl_iterator.h:297:5: note: template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
{ cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
^
In file included from /usr/include/c++/4.8.3/bits/stl_algobase.h:67:0,
from /usr/include/c++/4.8.3/bits/char_traits.h:39,
from /usr/include/c++/4.8.3/ios:40,
from /usr/include/c++/4.8.3/ostream:38,
from /usr/include/c++/4.8.3/iostream:39,
from grade.cpp:4:
/usr/include/c++/4.8.3/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator<(const reverse_iterator<_IteratorL>& __x,
^
/usr/include/c++/4.8.3/bits/stl_iterator.h:347:5: note: template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
{ cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
^
In file included from /usr/include/c++/4.8.3/string:52:0,
from /usr/include/c++/4.8.3/bits/locale_classes.h:40,
from /usr/include/c++/4.8.3/bits/ios_base.h:41,
from /usr/include/c++/4.8.3/ios:42,
from /usr/include/c++/4.8.3/ostream:38,
from /usr/include/c++/4.8.3/iostream:39,
from grade.cpp:4:
/usr/include/c++/4.8.3/bits/basic_string.h:2569:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/4.8.3/bits/basic_string.h:2569:5: note: template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
{ cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
^
In file included from /usr/include/c++/4.8.3/string:52:0,
from /usr/include/c++/4.8.3/bits/locale_classes.h:40,
from /usr/include/c++/4.8.3/bits/ios_base.h:41,
from /usr/include/c++/4.8.3/ios:42,
from /usr/include/c++/4.8.3/ostream:38,
from /usr/include/c++/4.8.3/iostream:39,
from grade.cpp:4:
/usr/include/c++/4.8.3/bits/basic_string.h:2581:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/4.8.3/bits/basic_string.h:2581:5: note: template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
{ cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
^
In file included from /usr/include/c++/4.8.3/string:52:0,
from /usr/include/c++/4.8.3/bits/locale_classes.h:40,
from /usr/include/c++/4.8.3/bits/ios_base.h:41,
from /usr/include/c++/4.8.3/ios:42,
from /usr/include/c++/4.8.3/ostream:38,
from /usr/include/c++/4.8.3/iostream:39,
from grade.cpp:4:
/usr/include/c++/4.8.3/bits/basic_string.h:2593:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<(const _CharT* __lhs,
^
/usr/include/c++/4.8.3/bits/basic_string.h:2593:5: note: template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note: mismatched types ‘const _CharT*’ and ‘std::basic_ostream<char>’
{ cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
you have to link it against the file to do this both must be compiled. e.g. g++ -Wall -Wextra grade.cpp Gradebook.cpp -o grade see how to link header files in c++

Operator<< for std::string

I define the operator<< for std::string objects:
std::string & operator<< (std::string & left, std::string & right){
return left += right;
}
Then I use it:
std::string t1("t1"), t2("t2"), t3;
t3 = t2 << t1;
And get from a compiler:
t.cpp: In function 'int main()':
t.cpp:44:28: error: no matching function for call to 'operator<<(std::string&, std::string&)'
t.cpp:44:28: note: candidates are:
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
from connection.h:10,
from login.cpp:1:
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT
, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template argument deduction/substitution failed:
t.cpp:44:28: note: 'std::string {aka std::basic_string<char>}' is not derived from 'std::basic_ostream<_CharT, _Traits>'
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
Why talks it about ostream and does not talk about string? I.e. why it does not take in account my definition of the operator<< ?
Thanks.
Update. For those who is able just to say "why do you create operator<< for strings?" and is not able to say any helpful things:
std::string & operator<< (std::string & left, const int num){
return left += std::to_string(num);
}
std::string t3;
t3 << 3 << 5;
std::cout << t3 << std::endl;
And log:
t.cpp: In function 'int main()':
t.cpp:45:12: error: no match for 'operator<<' in 't3 << 3'
t.cpp:45:12: note: candidates are:
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
from connection.h:10,
from login.cpp:1:
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT
, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template argument deduction/substitution failed:
t.cpp:45:12: note: 'std::string {aka std::basic_string<char>}' is not derived from 'std::basic_ostream<_CharT, _Traits>'
It does work:
#include <iostream>
#include <string>
std::string & operator<< (std::string & left, std::string & right){
return left += right;
}
int main()
{
std::string t1("t1"), t2("t2"), t3;
t3 = t2 << t1;
std::cout << t3;
}
Output: t2t1 [GCC 4.8.1]
The compiler output, as you say yourself, indicates that your operator overload is not even visible. You must not have your declaration in the right place.
This is not really a good idea, anyway: you will simply confuse the heck out of anybody who reads your code. Strings are not streams.