Errors on School Compiler

Started by
4 comments, last by Chad Smith 11 years, 2 months ago

EDIT: This has been fixed. I instead just created a stringstream buffer and filled out the strings that way instead of doing it on one line. Guess I should had known that their compiler doesn't support this yet...

Yes this is a school assignment that I was going to go ahead and try to compile on the schools compilers just to make sure all the test cases and everything were met before I started to clean the code up for the final submission. Though it fails compiling on the schools submission compiler pretty bad.

This was compiled with Visual Studio 2012 and it compilers perfectly with absolutely zero warnings. When using the schools submission compiler (was never told what compiler it is using) it had brought up errors like the following:


Main.cpp: In function 'void Add(std::string, Student*, unsigned int&)':
Main.cpp:94:35: error: no match for 'operator>>' in 'std::basic_stringstream((*(const __string_type*)(& properties)), std::operator|((std::_Ios_Openmode)16u, (std::_Ios_Openmode)8u)) >> firstName'
Main.cpp:94:35: note: candidates are:
/usr/include/c++/4.6/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream]
/usr/include/c++/4.6/istream:122:7: note:   no known conversion for argument 1 from 'std::string {aka std::basic_string}' to 'std::basic_istream::__istream_type& (*)(std::basic_istream::__istream_type&) {aka std::basic_istream& (*)(std::basic_istream&)}'
/usr/include/c++/4.6/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios]
/usr/include/c++/4.6/istream:126:7: note:   no known conversion for argument 1 from 'std::string {aka std::basic_string}' to 'std::basic_istream::__ios_type& (*)(std::basic_istream::__ios_type&) {aka std::basic_ios& (*)(std::basic_ios&)}'
/usr/include/c++/4.6/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream]
/usr/include/c++/4.6/istream:133:7: note:   no known conversion for argument 1 from 'std::string {aka std::basic_string}' to 'std::ios_base& (*)(std::ios_base&)'
/usr/include/c++/4.6/istream:169:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream]
/usr/include/c++/4.6/istream:169:7: note:   no known conversion for argument 1 from 'std::string {aka std::basic_string}' to 'bool&'
/usr/include/c++/4.6/istream:173:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits]

Here is the function it is complaining about:


void Add(std::string properties, Student theStudents[], unsigned int& numberElements)
{
	// temporary attributes to read in from the properties string
	std::string firstName, lastName, idNumber, classification, major;
	
	// fill the attributes from the information in the properties string
	std::stringstream(properties) >> firstName >> lastName >> idNumber >> classification >> major;

	bool alreadyAdded = false;

	// Only create the student if the idNumber is not already assigned
	for(unsigned int i = 0; i<numberElements; i++)
	{
		if(idNumber == theStudents[i].GetProperty(Student::IdNumber))
		{
			std::cout << "Error! ID Number already assigned." <<std::endl;
			alreadyAdded = true;
		}
	}

	if(!alreadyAdded)
	{
		theStudents[numberElements].Create(firstName, lastName, idNumber, classification, major);
		numberElements++;
	}
}

Visual Studio 2012 has absolutely zero problem with this.

From the error it seems it does not like me to do this:


// fill the attributes from the information in the properties string
	std::stringstream(properties) >> firstName >> lastName >> idNumber >> classification >> major;

Though I'm lost on why Visual Studio 2012 has no problem with that but their compiler absolutely hates that? Maybe I've always been wrong to do that really that's how I've always done it and not 100% sure of a clean solution to not do that.

Any ideas on why their compiler throws a fit about this but Visual Studio 2012 doesn't?

EDIT: I just tested it out using latest version of gcc and it complained about it also. What's Visual Studio 2012 compiler doing different here that it has zero problem? have I been using a C++11 feature of stringstream and actually not know it and the other compilers don't have that feature implemented yet? I'd appreciate any help here.

Thanks.

NOTE: The assignment doesn't have to deal with this command I/O, I've already implemented, tested, and finished the assignments major details.

Advertisement

Change your code to actually create a stringstream object


std::stringstream ss(properties);
ss>> firstName;

http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

From what I can gather, and I'm not sure if this is right, there is no declaration of


istream& operator>>(string& );

which is what you are trying to use it as.

Try using


istream& get ( char* s, streamsize n, char delim );

instead.

Yo dawg, don't even trip.

Change your code to actually create a stringstream object


std::stringstream ss(properties);
ss>> firstName;

Yea that's what I ended up doing instead when I actually thought about what I was actually doing. Guess that's what I get for getting used to one single compiler too much and not knowing what different compilers features have right now.

Any ideas on why their compiler throws a fit about this but Visual Studio 2012 doesn't?

This is legal C++11 code, but not legal C++03 code. In C++11 there's a operator>> overload that takes an rvalue reference to a std::istream as the left hand side. In C++03 there's only a operator>> overload for a non-const lvalue reference to a std::istream on the left hand side, and a temporary can't be bound to a non-const lvalue reference. IIRC, even the non-C++11 versions of MSVC will accept this kind of code because they have an extension that allows binding a temporary to a non-const lvalue reference.

Any ideas on why their compiler throws a fit about this but Visual Studio 2012 doesn't?

This is legal C++11 code, but not legal C++03 code. In C++11 there's a operator>> overload that takes an rvalue reference to a std::istream as the left hand side. In C++03 there's only a operator>> overload for a non-const lvalue reference to a std::istream on the left hand side, and a temporary can't be bound to a non-const lvalue reference. IIRC, even the non-C++11 versions of MSVC will accept this kind of code because they have an extension that allows binding a temporary to a non-const lvalue reference.

Thanks for letting me know about this. That seems like it would answer my question fully on the subject. Appreciate it.

This topic is closed to new replies.

Advertisement