Related
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];
^
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.
I am trying to take a simple CSV file, split it line-by-line, and print it to the console. Currently I am getting an error when compiling and want to know if I am missing something obvious.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
int main(int argc , char** argv) {
std::string line;
std::ifstream infile(argv[1]);
if (infile) {
while (getline(infile, line)) {
std::istringstream ss(line);
std::string token;
while(std::getline(ss, token, ",")) {
std::cout << token << "\n";
}
}
}
infile.close();
return 0;
}
The error I am getting is as follows.
csv.cpp: In function 'int main(int, char**)':
csv.cpp:41:46: error: no matching function for call to 'getline(std::istringstream&,
std::string&, const char [2])'
csv.cpp:41:46: note: candidates are:
In file included from /usr/include/c++/4.7/string:55:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from csv.cpp:21:
/usr/include/c++/4.7/bits/basic_string.tcc:1070: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)
/usr/include/c++/4.7/bits/basic_string.tcc:1070:5: note: template argument deduction/substitution failed:
csv.cpp:41:46: note: deduced conflicting types for parameter '_CharT' ('char' and 'const char*')
In file included from /usr/include/c++/4.7/string:54:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from csv.cpp:21:
/usr/include/c++/4.7/bits/basic_string.h:2792: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>&)
/usr/include/c++/4.7/bits/basic_string.h:2792:5: note: template argument deduction/substitution failed:
csv.cpp:41:46: note: candidate expects 2 arguments, 3 provided
The third parameter of getline is a char, not a char*. Make it getline(ss, token, ',') - note single quotes.
Oh, and beware CSV fields "like"",""this" (in case you wonder, this is a single field with the value of like","this). There's more to CSV syntax than meets the eye.
Perhaps you should use a library for CSV munging... perhaps this question is useful.
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);
}
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.