new to C++ : Error: no match for `operator` ....

This is a discussion on new to C++ : Error: no match for `operator` .... within the C++ Programming forums, part of the General Programming Boards category; hi guys, I'm fairly new to C++ and have struck my first problem. I am developing a simple program where ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    Question new to C++ : Error: no match for `operator` ....

    hi guys,

    I'm fairly new to C++ and have struck my first problem.

    I am developing a simple program where I input a string and a float value, and they are written to a text file.

    It was working fine until I've put in my if and else statements to check the inputted string and float are of a correct value.

    here's my script:
    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std ;
    
    // conflicting forces version
    
    int main()
    {
    	string game = "Conflicting Forces";
    	float version;
    	string stage;
    	
    	// enter stage
    	cout << "Enter Conflicting Forces Version Stage" << endl;
    	cin >> stage;
    	
    	if(stage == "Beta" || stage == "Alpha" || stage == "Live")
    	{
    		// enter version number
    		cout << "Enter Conflicting Forces Version Number" << endl;
    		cin >> version;
    		
    		if(version > 1.0 && version < 3.0)
    		{
    				cout << "Conflicting Forces is now version: " + stage + " " + version << endl;
    		}
    		else
    		{
    			cout << "Error: Not a valid version" << endl;
    		}
    	}
    	else
    	{
    		cout << "Error: Not a valid stage" << endl;
    	}
    	
    	ofstream writer("cf-version.txt");
    	
    	if(!writer)
    	{
    	 cout << "Error opening file for output" << endl;
    	 return -1;
    	}
    	else
    	{
    	 writer << (game + " Version " + stage + version) << endl;
    	 writer.close();
    	}
    	  
    	// stop window from closing after the print
    	system("pause");
    	return 0 ;
    }
    here's my error when compiling:
    cf-version.cpp: In function `int main()':
    cf-version.cpp:26: error: no match for 'operator+' in '
    :basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
    aits = std::char_traits<char>, _Alloc = std::allocator<
    ) + version'
    cf-version.cpp:47: error: no match for 'operator+' in '
    :basic_string<_CharT, _Traits, _Alloc>&, const std::bas
    , _Alloc>&) [with _CharT = char, _Traits = std::char_tr
    :allocator<char>](((const std::basic_string<char, std::
    llocator<char> >&)((const std::basic_string<char, std::
    llocator<char> >*)(&stage)))) + version'
    cf-version.cpp:54:2: warning: no newline at end of file
    Everytime I compile a file I always get this warning:
    cf-version.cpp:54:2: warning: no newline at end of file
    however it doesn't appear to affect my program, is there a simple solution to get rid of this warning?
    and what do I need to change to get rid of the rest of the errors?

    sorry for the not so well formatted error. I am compiling in command prompt using MinGW on Windows

    thanks for any help in advance

    Regards ACE
    Last edited by MasterACE14; 05-22-2008 at 10:05 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    however it doesn't appear to affect my program, is there a simple solution to get rid of this warning?
    Add a new (blank) line at the end of the file.

    It is more for backward compatibility, as modern compilers IIRC don't really need the newline anymore.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Code:
    cout << "Conflicting Forces is now version: " + stage + " " + version << endl;
    Something like this doesn't work. Should be
    Code:
    cout << "Conflicting Forces is now version: " << stage << " " << version << endl;
    However, later on in the code
    Code:
    writer << (game + " Version " + stage + version) << endl;
    works because game comes first, so it's operator+ is called to combine strings. A string literal (a string in double quotes) doesn't have that.

    For more information, google "operator overloading"

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    7
    after changing that I'm still left with errors. I've been reading about operator overloading, but it doesn't really make sense to me :S

    the blank line at the end has removed the warning

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    after changing that I'm still left with errors. I've been reading about operator overloading, but it doesn't really make sense to me :S
    Then don't worry about it for now (operator overloading), it is a semi-advanced topic. Just avoid adding strings.

    What errors are you now getting?

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    7
    heres the errors

    C:\MyPrograms>c++ cf-version.cpp -o cf-version-beta.exe
    cf-version.cpp: In function `int main()':
    cf-version.cpp:47: error: no match for 'operator+' in 'std:perator+(const std:
    :basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits
    , _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std:
    :allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::a
    llocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::a
    llocator<char> >*)(&stage)))) + version'

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I am guessing you are still adding strings on this line
    Code:
    writer << (game + " Version " + stage + version) << endl;

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    7
    hmm, yeah I don't really know about the concocting stuff. I'm a long time PHP programmer, I'm more use to
    PHP Code:
    $var "word" $var2 
    but C++ doesn't appear to work in such a simple way. It looks more like javascript with the + signs.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Yeap, but that is PHP. This is C++.

    Code:
    writer << game << " Version " << stage << version << endl;
    You can add strings, too, with some restrictions. But just do it this way until you learn about operator overloading.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    7
    ok, its compiling alright now. With the first question "what stage", that works fine with the if statement.

    But with the second if statement "what version number" it just goes to the else if I put something in like 1.0 for example. I then tried changing the variable scope for that, from a float to a string, but get a error then with the second if statement.

    any ideas?

    I've changed the if statement to this...

    if(version >= 1 && version <= 3)

    but its throwing me this error when compiling.

    cf-version.cpp: In function `int main()':
    cf-version.cpp:25: error: no match for 'operator>=' in 'version >= 1'
    cf-version.cpp:25: error: no match for 'operator<=' in 'version <= 3'
    Last edited by MasterACE14; 05-22-2008 at 11:54 PM.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    floats do not have infinite precision. That is to say, 1.0 may be represented as something like 1.0000000000001 or 0.999999999.

    The solution is to use a threshold when comparing floating point numbers.
    http://www.cygnus-software.com/paper...%20numbers.htm

    Edit: actually, come to think of it, even if floats do have infinite precision, 1.0 would make the if statement go to else. (1.0 > 1.0) is not true. I think you meant to use >= instead of >.

    You cannot compare floats contained in a string with a >.

    C++ is a strongly typed language. You cannot do all the PHP fun stuff here.
    Last edited by cyberfish; 05-22-2008 at 11:50 PM.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    7
    the version number will only ever be something between 1.0 and 3.0

    is there another way to achieve this if statement, maybe not use float or double, I don't know.

    maybe as a Integer and break the version number into 2 integer variables. full number, and then the number past the decimal .0 or whatever, put them into the same cin, but in command prompt have a dot . in the middle of the cin to make it look like a decimal. So its actually 1 integer, then dot, then another integer when its written to the file?
    Last edited by MasterACE14; 05-23-2008 at 12:22 AM.

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    maybe as a Integer and break the full number, and then the number past the decimal .0 or whatever, put them into the same cin, but in command prompt have a dot . in the middle of the cin to make it look like a decimal. So its actually 1 integer, then dot, then another integer when its written to the file?
    If you know there will only be one digit after the decimal place, you can use fixed point arithmetics (using an integer that is multiplied by 10). Eg, 1.0 becomes 10, 0.5 becomes 5 etc.

    Using float should be fine, though. But you need to do it like this (as suggested in the article)
    Code:
    const float EPSILON = 1e-5; //an arbitrary small number
    if(version > (1.0 - EPSILON) && version < (3.0 + EPSILON))
    I will leave it to you why that works.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    7
    yep that works, I'm gonna look into this epsilon stuff.

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 06:37 PM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. 2 array match
    By ajastru2000 in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2003, 07:58 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21