compiling error

if i compile my following code in code blocks it compiles perfectly but when i try it in eclipse it shows a lot of errors. Can someone tell me what can i do?

my code is as follows****

#include<iostream>
#include<cstdlib>
#include <algorithm>
#include<ctime>
#include<iomanip>
#include<vector>
using namespace std;
int myrandom (int i) { return std::rand()%i;}
int card_to_move(vector<string> input) // function to check if the players has the same suit
{
int i =0; int j;
char suit = input.at(0).at(0);
for (auto comparison : input)
{
if (comparison.at(0) == suit){
i++;
}
else{j=i;break;}
}return j;
}
bool are_all_same_suit(vector<string> input)// function to compare suits
{
char suit = input.at(0).at(0);
for (auto comparison : input)
{
if (comparison.at(0) != suit) return false;
}
return true;
}
int main()
{

srand ( unsigned ( std::time(0) ) );
vector<string> myvector;
vector<string>all_cards;
vector<string>player_0;
vector<string>player_1;
vector<string>player_2;
vector<string>player_3;
vector<string>player_4;
vector<string>cards={"DA","DK","DQ","DJ","D2","D3","D4","D5","D6","D7","D8","D9","D10",
"HA","HK","HQ","HJ","H2","H3","H4","H5","H6","H7","H8","H9","H10",
"SA","SK","SQ","SJ","S2","S3","S4","S5","S6","S7","S8","S9","S10",
"CA","CK","CQ","CJ","C2","C3","C4","C5","C6","C7","C8","C9","C10"};// vector holding all the cards

// set some values:
for (int i=1; i<52; ++i) myvector.push_back(cards[i]);

// using built-in random generator:
random_shuffle ( myvector.begin(), myvector.end() );

// using myrandom:
random_shuffle ( myvector.begin(), myvector.end(), myrandom);// shuffling so that a cards is not generated twice
for (std::vector<string>::iterator it=myvector.begin(); it!=myvector.end(); ++it){
//std::cout << ' ' << *it;

all_cards.push_back(*it);}//all cards saved at all_cards
for(int i=0;i<7;i++){
player_0.push_back(all_cards[i]);
player_1.push_back(all_cards[i+9]);
player_2.push_back(all_cards[i+18]);
player_3.push_back(all_cards[i+27]);
player_4.push_back(all_cards[i+36]);
}
string x,a,b,c,d,e,f,g,y;
a=player_0[0];b=player_0[1];c=player_0[2];d=player_0[3];e=player_0[4];f=player_0[5];g=player_0[6];
cout<<"player_0 (a)"<<a<<setw(5)<<"(b)"<<b<<setw(5)<<"(c)"<<c<<setw(5)<<"(d)"<<d<<setw(5)<<"(e)"<<e<<setw(5)<<"(f)"<<f<<setw(5)<<"(g)"<<g<<endl;
char choice; // player_0' choice
cout << "which one to replace ?"<<endl;
while(cin >> choice)
{
cout << "which one to replace ?"<<endl;
char check;


switch (choice){ // switch the option the player is choosing
case 'a':
player_1.push_back(player_0[0]);
player_0.erase(player_0.begin());

break;
case 'b':
player_1.push_back(player_0[1]);
player_0.erase(player_0.begin()+1);

break;
case 'c':
player_1.push_back(player_0[2]);
player_0.erase(player_0.begin()+2);

break;
case 'd':
player_1.push_back(player_0[3]);
player_0.erase(player_0.begin()+3);

break;
case 'e':
player_1.push_back(player_0[4]);
player_0.erase(player_0.begin()+4);

break;
case 'f':
player_1.push_back(player_0[5]);
player_0.erase(player_0.begin()+5);

break;
case 'g':
player_1.push_back(g);
player_0.erase(player_0.begin()+6);

break;
case '?':
cout<<"player_0 (a)"<<a<<setw(5)<<"(b)"<<b<<setw(5)<<"(c)"<<c<<setw(5)<<"(d)"<<d<<setw(5)<<"(e)"<<e<<setw(5)<<"(f)"<<f<<setw(5)<<"(g)"<<g<<endl;
cout<<"player_1 (a)"<<player_1[0]<<setw(5)<<"(b)"<<player_1[1]<<setw(5)<<"(c)"<<player_1[2]<<setw(5)<<"(d)"<<player_1[3]<<setw(5)<<"(e)"<<player_1[4]<<setw(5)<<"(f)"<<player_1[5]<<setw(5)<<"(g)"<<player_1[6]<<endl;
cout<<"player_2 (a)"<<player_2[0]<<setw(5)<<"(b)"<<player_2[1]<<setw(5)<<"(c)"<<player_2[2]<<setw(5)<<"(d)"<<player_2[3]<<setw(5)<<"(e)"<<player_2[4]<<setw(5)<<"(f)"<<player_2[5]<<setw(5)<<"(g)"<<player_2[6]<<endl;
cout<<"player_3 (a)"<<player_3[0]<<setw(5)<<"(b)"<<player_3[1]<<setw(5)<<"(c)"<<player_3[2]<<setw(5)<<"(d)"<<player_3[3]<<setw(5)<<"(e)"<<player_3[4]<<setw(5)<<"(f)"<<player_3[5]<<setw(5)<<"(g)"<<player_3[6]<<endl;
cout<<"player_4 (a)"<<player_4[0]<<setw(5)<<"(b)"<<player_4[1]<<setw(5)<<"(c)"<<player_4[2]<<setw(5)<<"(d)"<<player_4[3]<<setw(5)<<"(e)"<<player_4[4]<<setw(5)<<"(f)"<<player_4[5]<<setw(5)<<"(g)"<<player_4[6]<<endl;
cout << endl;
cout << "which one to replace ?"<<endl;
break;
default:
break;

}
// checking if the player has the same suit of cards
if(are_all_same_suit(player_0)==true){
cout<<"player_0 wins"<<endl;
break;
}
else if(are_all_same_suit(player_1)==true){
cout<<"player_1 wins" <<endl;
break;
}
else if(are_all_same_suit(player_2)==true){
cout<<"player_2 wins" <<endl;
break;
}
else if(are_all_same_suit(player_3)==true){
cout<<"player_3 wins" <<endl;
break;
}
else if(are_all_same_suit(player_4)==true){
cout<<"player_4 wins" <<endl;
break;
}
else{ // if the player has mismatch cards they choose to obtain different cards
if(choice=='?'){continue;}
if(are_all_same_suit(player_1)!=true){
player_2.push_back(player_1[card_to_move(player_1)]);
player_1.erase(player_1.begin()+(card_to_move(player_1)));}
if(are_all_same_suit(player_2)!=true){
player_3.push_back(player_2[card_to_move(player_2)]);
player_2.erase(player_2.begin()+(card_to_move(player_2)));

}
if(are_all_same_suit(player_3)!=true){
player_4.push_back(player_3[card_to_move(player_3)]);
player_3.erase(player_3.begin()+(card_to_move(player_3)));

}
if(are_all_same_suit(player_4)!=true){
player_0.push_back(player_4[card_to_move(player_4)]);
player_4.erase(player_4.begin()+(card_to_move(player_4)));}
}a=player_0[0];b=player_0[1];c=player_0[2];d=player_0[3];e=player_0[4];f=player_0[5];g=player_0[6];
cout<<"(a)"<<a<<setw(5)<<"(b)"<<b<<setw(5)<<"(c)"<<c<<setw(5)<<"(d)"<<d<<setw(5)<<"(e)"<<e<<setw(5)<<"(f)"<<f<<setw(5)<<"(g)"<<g<<endl;
}
}
What error message do you get?
17:48:41 **** Incremental Build of configuration Debug for project my ship sails! ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\my ship sails!.o" "..\\src\\my ship sails!.cpp"
..\src\my ship sails!.cpp: In function 'int card_to_move(std::vector<std::__cxx11::basic_string<char> >)':
..\src\my ship sails!.cpp:13:10: warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]
for (auto comparison : input)
^
..\src\my ship sails!.cpp:13:15: error: 'comparison' does not name a type
for (auto comparison : input)
^
..\src\my ship sails!.cpp:19:10: error: expected ';' before 'return'
}return j;
^
..\src\my ship sails!.cpp:19:10: error: expected primary-expression before 'return'
..\src\my ship sails!.cpp:19:10: error: expected ';' before 'return'
..\src\my ship sails!.cpp:19:10: error: expected primary-expression before 'return'
..\src\my ship sails!.cpp:19:10: error: expected ')' before 'return'
..\src\my ship sails!.cpp:11:9: warning: unused variable 'i' [-Wunused-variable]
int i =0; int j;
^
..\src\my ship sails!.cpp:12:10: warning: unused variable 'suit' [-Wunused-variable]
char suit = input.at(0).at(0);
^
..\src\my ship sails!.cpp: In function 'bool are_all_same_suit(std::vector<std::__cxx11::basic_string<char> >)':
..\src\my ship sails!.cpp:24:10: warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]
for (auto comparison : input)
^
..\src\my ship sails!.cpp:24:15: error: 'comparison' does not name a type
for (auto comparison : input)
^
..\src\my ship sails!.cpp:28:5: error: expected ';' before 'return'
return true;
^
..\src\my ship sails!.cpp:28:5: error: expected primary-expression before 'return'
..\src\my ship sails!.cpp:28:5: error: expected ';' before 'return'
..\src\my ship sails!.cpp:28:5: error: expected primary-expression before 'return'
..\src\my ship sails!.cpp:28:5: error: expected ')' before 'return'
..\src\my ship sails!.cpp:23:10: warning: unused variable 'suit' [-Wunused-variable]
char suit = input.at(0).at(0);
^
..\src\my ship sails!.cpp: In function 'int main()':
..\src\my ship sails!.cpp:44:94: error: in C++98 'cards' must be initialized by constructor, not by '{...}'
"CA","CK","CQ","CJ","C2","C3","C4","C5","C6","C7","C8","C9","C10"};// vector holding all the cards
^
..\src\my ship sails!.cpp:44:94: error: could not convert '{"DA", "DK", "DQ", "DJ", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "HA", "HK", "HQ", "HJ", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "SA", "SK", "SQ", "SJ", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "CA", "CK", "CQ", "CJ", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"}' from '<brace-enclosed initializer list>' to 'std::vector<std::__cxx11::basic_string<char> >'
..\src\my ship sails!.cpp:73:10: warning: unused variable 'check' [-Wunused-variable]
char check;
^

17:48:44 Build Finished (took 3s.400ms)
Last edited on
When you get a bunch of error like this you should always start at the top. Often later errors will just go away when you remove an earlier error.

The first warning and the first error talks about this line.
 
for (auto comparison : input)

This line of code uses two new features that was added C++ in C++11 (the 2011 version of the C++ standard).
* automatic type detection
* range-based for loops

If you want to use C++11 features you need to pass the compiler flag -std=c++11 to g++.
Last edited on
Also, turn on as many warnings options as possible, they are your friend:

http://www.cplusplus.com/forum/general/183731/#msg899203

So, at least:

g++ -std=c++11 -Wall -Wextra -pedantic-errors

-std=c++14 if you have it, gcc 5.0 or later.

This is probably the reason why you had different results from one IDE to another, the default warning level was different. Some IDE's have no default complier warning options at all, not even -Wall - which is crazy IMO.

Good Luck !!

Edit:
Please always use code tags:
http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Topic archived. No new replies allowed.