Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Errors on School Compiler


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Chad Smith   Members   -  Reputation: 1139

Like
0Likes
Like

Posted 30 January 2013 - 03:37 PM

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.


Edited by Chad Smith, 30 January 2013 - 04:09 PM.


Sponsor:

#2 Yourself   Crossbones+   -  Reputation: 1144

Like
0Likes
Like

Posted 30 January 2013 - 04:11 PM

Change your code to actually create a stringstream object

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


#3 boogyman19946   Members   -  Reputation: 1071

Like
0Likes
Like

Posted 30 January 2013 - 04:16 PM

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.


"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#4 Chad Smith   Members   -  Reputation: 1139

Like
0Likes
Like

Posted 30 January 2013 - 04:39 PM

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.



#5 SiCrane   Moderators   -  Reputation: 9626

Like
1Likes
Like

Posted 30 January 2013 - 06:46 PM

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.

#6 Chad Smith   Members   -  Reputation: 1139

Like
0Likes
Like

Posted 30 January 2013 - 09:57 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS