Joel Joel - 3 months ago 13
C++ Question

Compiler error on windows but not on mac

I'm running the same code on my laptop which is a mac and it works fine. (not using any specific mac-libraries). Now that i'm trying to send the code to my pc, i get ~300lines of errors.

CMD-OUTPUT:

C:\Users\joel\Desktop\Hej>g++ -std=c++11 dice.cpp
dice.cpp: In member function 'std::string dice::getOne()':
dice.cpp:28:19: error: ambiguous overload for 'operator=' (operand types are 'st
d::string {aka std::basic_string<char>}' and 'std::basic_ostream<char>::__ostrea
m_type {aka std::basic_ostream<char>}')
return one = cout << "------" << "\n! !\n! x !\n! !\n" << "---
---" << endl; //one
^
dice.cpp:28:19: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52:0,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_cla
sses.h:40,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h
:41,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
from dice.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<cha
r>; _Alloc = std::allocator<char>] <near match>
operator=(const _CharT* __s)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'const char*'
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Al
loc = std::allocator<char>] <near match>
operator=(_CharT __c)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'char'
dice.cpp: In member function 'std::string dice::getTwo()':
dice.cpp:31:19: error: ambiguous overload for 'operator=' (operand types are 'st
d::string {aka std::basic_string<char>}' and 'std::basic_ostream<char>::__ostrea
m_type {aka std::basic_ostream<char>}')
return two = cout << "------" << "\n! x!\n! !\n!x !\n" << "---
---" << endl; //two
^
dice.cpp:31:19: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52:0,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_cla
sses.h:40,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h
:41,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
from dice.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<cha
r>; _Alloc = std::allocator<char>] <near match>
operator=(const _CharT* __s)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'const char*'
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Al
loc = std::allocator<char>] <near match>
operator=(_CharT __c)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'char'
dice.cpp: In member function 'std::string dice::getThree()':
dice.cpp:34:21: error: ambiguous overload for 'operator=' (operand types are 'st
d::string {aka std::basic_string<char>}' and 'std::basic_ostream<char>::__ostrea
m_type {aka std::basic_ostream<char>}')
return three = cout << "------" << "\n!x !\n! x !\n! x!\n" << "
------" << endl; //three
^
dice.cpp:34:21: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52:0,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_cla
sses.h:40,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h
:41,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
from dice.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<cha
r>; _Alloc = std::allocator<char>] <near match>
operator=(const _CharT* __s)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'const char*'
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Al
loc = std::allocator<char>] <near match>
operator=(_CharT __c)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'char'
dice.cpp: In member function 'std::string dice::getFour()':
dice.cpp:37:20: error: ambiguous overload for 'operator=' (operand types are 'st
d::string {aka std::basic_string<char>}' and 'std::basic_ostream<char>::__ostrea
m_type {aka std::basic_ostream<char>}')
return four = cout << "------" << "\n! x x !\n! !\n! x x !\n" << "-
-----" << endl; //four
^
dice.cpp:37:20: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52:0,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_cla
sses.h:40,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h
:41,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
from dice.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<cha
r>; _Alloc = std::allocator<char>] <near match>
operator=(const _CharT* __s)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'const char*'
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Al
loc = std::allocator<char>] <near match>
operator=(_CharT __c)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'char'
dice.cpp: In member function 'std::string dice::getFive()':
dice.cpp:40:20: error: ambiguous overload for 'operator=' (operand types are 'st
d::string {aka std::basic_string<char>}' and 'std::basic_ostream<char>::__ostrea
m_type {aka std::basic_ostream<char>}')
return five = cout << "------" << "\n! x x !\n! x !\n! x x !\n" << "--
----" << endl; //five
^
dice.cpp:40:20: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52:0,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_cla
sses.h:40,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h
:41,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
from dice.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<cha
r>; _Alloc = std::allocator<char>] <near match>
operator=(const _CharT* __s)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'const char*'
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Al
loc = std::allocator<char>] <near match>
operator=(_CharT __c)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'char'
dice.cpp: In member function 'std::string dice::getSix()':
dice.cpp:43:18: error: ambiguous overload for 'operator=' (operand types are 'st
d::string {aka std::basic_string<char>}' and 'std::basic_ostream<char>::__ostrea
m_type {aka std::basic_ostream<char>}')
return six = cout << "------" << "\n! x x !\n! x x !\n! x x !\n" << "---
---" << endl; //six
^
dice.cpp:43:18: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52:0,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_cla
sses.h:40,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h
:41,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
from dice.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<cha
r>; _Alloc = std::allocator<char>] <near match>
operator=(const _CharT* __s)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:554:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'const char*'
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: std:
:basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Allo
c>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Al
loc = std::allocator<char>] <near match>
operator=(_CharT __c)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_string.h:565:7: note: no
known conversion for argument 1 from 'std::basic_ostream<char>::__ostream_type
{aka std::basic_ostream<char>}' to 'char'
dice.cpp: In function 'int main()':
dice.cpp:63:20: error: 'numeric_limits' is not a member of 'std'
if (cin.ignore(std::numeric_limits<streamsize>::max(),'\n')){
^
dice.cpp:63:50: error: expected primary-expression before '>' token
if (cin.ignore(std::numeric_limits<streamsize>::max(),'\n')){
^
dice.cpp:63:57: error: no matching function for call to 'max()'
if (cin.ignore(std::numeric_limits<streamsize>::max(),'\n')){
^
dice.cpp:63:57: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\vector:60:0,
from dice.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:216:5: note: temp
late<class _Tp> const _Tp& std::max(const _Tp&, const _Tp&)
max(const _Tp& __a, const _Tp& __b)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:216:5: note: te
mplate argument deduction/substitution failed:
dice.cpp:63:57: note: candidate expects 2 arguments, 0 provided
if (cin.ignore(std::numeric_limits<streamsize>::max(),'\n')){
^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\vector:60:0,
from dice.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:260:5: note: temp
late<class _Tp, class _Compare> const _Tp& std::max(const _Tp&, const _Tp&, _Com
pare)
max(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:260:5: note: te
mplate argument deduction/substitution failed:
dice.cpp:63:57: note: candidate expects 3 arguments, 0 provided
if (cin.ignore(std::numeric_limits<streamsize>::max(),'\n')){
^


File:

//dice
#include <vector>
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

class dice{
private:
string one;
string two;
string three;
string four;
string five;
string six;
public:
dice(){srand(time(NULL));};
dice(string one, string two, string three, string four, string five, string six){
this->one=one;
this->two=two;
this->three=three;
this->four=four;
this->five=five;
this->six=six;
}
string getOne(){
return one = cout << "------" << "\n! !\n! x !\n! !\n" << "------" << endl; //one
}
string getTwo(){
return two = cout << "------" << "\n! x!\n! !\n!x !\n" << "------" << endl; //two
}
string getThree(){
return three = cout << "------" << "\n!x !\n! x !\n! x!\n" << "------" << endl; //three
}
string getFour(){
return four = cout << "------" << "\n! x x !\n! !\n! x x !\n" << "------" << endl; //four
}
string getFive(){
return five = cout << "------" << "\n! x x !\n! x !\n! x x !\n" << "------" << endl; //five
}
string getSix(){
return six = cout << "------" << "\n! x x !\n! x x !\n! x x !\n" << "------" << endl; //six
}
};



int main(){ //main

dice myDice = dice(); //referens
string c, stop="stop";


while(1){
cout << "Generate two random dice: press [ENTER]\n";

//random funktion nedan

string c;


if (cin.ignore(std::numeric_limits<streamsize>::max(),'\n')){

for(int i=0;i<2;i++){ //select two random numbers
int num;

int min=1,max=6;
num = (rand() % max + min);
//if
if (num==1){
myDice.getOne();
}else if(num==2){
myDice.getTwo();
}else if(num==3){
myDice.getThree();
}else if(num==4){
myDice.getFour();
}else if(num==5){
myDice.getFive();
}else if(num==6){
myDice.getSix();
}else{
cout << "higher than 6 or lower than 1. If that happens, i'm quitting programming (may this function never be used)\n";
}
}
}
}
return 0;
}


What's causing this? I'm using g++ to compile on windows. Can someone please try to run this code on windows and confirm that it's indeed windows-related?

Answer

I have a mac, and compiling on said mac yields:

/Users/rhodges/play/print/print/main.cpp:28:21: error: no viable overloaded '='
        return  one = cout << "------" << "\n!     !\n!  x  !\n!     !\n" << "------" << endl; //one
                ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Users/rhodges/play/print/print/main.cpp:3:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1376:19: note: candidate function not viable: no known conversion from 'std::__1::basic_ostream<char>' to 'const std::__1::basic_string<char>' for 1st argument
    basic_string& operator=(const basic_string& __str);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1379:19: note: candidate function not viable: no known conversion from 'std::__1::basic_ostream<char>' to 'std::__1::basic_string<char>' for 1st argument
    basic_string& operator=(basic_string&& __str)
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1383:45: note: candidate function not viable: no known conversion from 'std::__1::basic_ostream<char>' to 'const value_type *' (aka 'const char *') for 1st argument
    _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
                                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1384:19: note: candidate function not viable: no known conversion from 'std::__1::basic_ostream<char>' to 'value_type' (aka 'char') for 1st argument
    basic_string& operator=(value_type __c);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1387:19: note: candidate function not viable: no known conversion from 'std::__1::basic_ostream<char>' to 'initializer_list<value_type>' (aka 'initializer_list<char>') for 1st argument
    basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}

six times over

this is invalid:

 string getOne(){
      return  one = cout << "------" << "\n!     !\n!  x  !\n!     !\n" << "------" << endl; //one
    }

std::cout << XXX returns a std::ostream&, not a std::string.

what will work is:

string getOne(){
  if (one.empty())
  {
    std::ostringstream ss;
    ss << "------" << "\n!     !\n!  x  !\n!     !\n" << "------" << endl; //one
    one = ss.str();
  }
  return one;
}

You'll need to #include <sstream>

It might actually be easier to implement it this way (fewer moving parts):

//dice
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <array>

using namespace std;

class dice{
private:
    static const char* const one;
    static const char* const two;
    static const char* const three;
    static const char* const four;
    static const char* const five;
    static const char* const six;
    static const char* const texts_[6];
public:
    dice(){
        srand(unsigned(time(NULL)));
    };

    std::ostream& show(std::ostream& os, int value) const
    {
        if (value < 1 || value > 6)
            return os << "invalid value\n";
        return os << texts_[value - 1];
    }
};

const char* const dice::one =
"------\n"
"!     !\n"
"!  x  !\n"
"!     !\n"
"------\n";

const char* const dice::two =
"------\n"
"!x    !\n"
"!     !\n"
"!    x!\n"
"------\n";

const char* const dice::three =
"------\n"
"!x    !\n"
"!  x  !\n"
"!    x!\n"
"------\n";

const char* const dice::four =
"------\n"
"! x x !\n"
"!     !\n"
"! x x !\n"
"------\n";

const char* const dice::five =
"------\n"
"! x x !\n"
"!  x  !\n"
"! x x !\n"
"------\n";

const char* const dice::six =
"------\n"
"! x x !\n"
"! x x !\n"
"! x x !\n"
"------\n";

const char* const dice::texts_[6] {
    one, two, three, four, five, six
};


int main(){ //main

    dice myDice = dice(); //referens
    string c, stop="stop";


    while(1){
        cout << "Generate two random dice: press [ENTER]\n";

        //random funktion nedan

        string c;


        if (cin.ignore(std::numeric_limits<streamsize>::max(),'\n')){

            for(int i=0;i<2;i++){ //select two random numbers
                int num;

                int min=1,max=6;
                num = (rand() % max + min);
                myDice.show(std::cout, num);
            }
        }
    }
    return 0;
}
Comments