我在我的笔记本电脑上运行相同的代码,这是一个mac,工作正常。 (不使用任何特定的mac库)。现在我正在尝试将代码发送到我的电脑,我得到了300行错误。
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')){
^
文件:
//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;
}
造成这种情况的原因是什么?我正在使用g ++在windows上编译。有人可以尝试在Windows上运行此代码并确认它确实与Windows相关吗?
答案 0 :(得分:2)
我有一个mac,并在所述mac产品上进行编译:
/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());}
六次
这是无效的:
string getOne(){
return one = cout << "------" << "\n! !\n! x !\n! !\n" << "------" << endl; //one
}
std::cout << XXX
会返回std::ostream&
,而不是std::string
。
将起作用的是:
string getOne(){
if (one.empty())
{
std::ostringstream ss;
ss << "------" << "\n! !\n! x !\n! !\n" << "------" << endl; //one
one = ss.str();
}
return one;
}
您需要#include <sstream>
以这种方式实现它可能更容易(运动部件更少):
//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;
}