Question:
C++ problem............................................................?
Sean
2009-05-01 04:54:28 UTC
This should be so easy to! wtf!

I get the following warning: Warning 6 warning C4832: token '.' is illegal after UDT 'players'

on line "27"

For the following ~simple~ syntax

#include
#include
#include

using std::cout;
using std::cin;
using std::endl;
using std::string;

struct players
{
string name;
const int age;
int wins;
int loses;
};

int main(void)
{
players
{
name = "Sean",
age = 16,
wins = 7;
loses = 0;
};
cout << players.name;
}

wuuut duhhh fawk?
Six answers:
Germann A
2009-05-01 05:38:04 UTC
1) You called your struct (definition) AND your variable (instance of the struct) identical names. My guess that the compiler does not understand what you are trying to do (in your main()) - either redefine the struct or assign values to new object/instance...
2009-05-01 12:34:45 UTC
Classes and structs are User Defined types that mean that The name of the class or struct will be a type just like int, double, char,string

you canot use a string like

string.data = "123";



right?



You first have to make an object of that type



players currentPlayer;

//.....

cout << currentPlater.name << "\n";
cja
2009-05-01 18:44:59 UTC
Here's an easy way to do it:



#include

#include



using namespace std;



struct player {

    player(const string &n, int a, int w, int l)

        : name(n), age(a), wins(w), losses(l) { }

    string name;

    const int age;

    int wins;

    int losses;

};



ostream &operator<<(ostream &os, const player &p) {

    os << p.name << ": " << p.age << ", ";

    os << p.wins << "-" << p.losses;

    return os;

}



int main(int argc, char *argv[]) {

    player p("Sean",16,7,0);

    cout << p << endl;

    return 0;

}



#if 0



Program output:



Sean: 16, 7-0



#endif
Ciaron
2009-05-01 13:49:47 UTC
The compiler is confused as the structure is a global you don't need to worry about that, I would have just set it up like that

players.name="Sean";

players.age=16;

players.wins=7;

player.lose=0;

that should work it's a bit messy but it should work and all you need to do is tweak it.
dentonez
2009-05-01 12:27:22 UTC
I haven't worked with structs since college, but I'm pretty sure you can't instantiate your variables like that.... try





int main(void)

{

players p1;



p1.name = "Sean",

p1.age = 16,

p1.wins = 7;

p1.loses = 0;



cout << players.name;



}



OR MAYBE



int main(void)

{

struct players p1 = {name = "Sean", age = 16, wins = 7, loses = 0};



cout << players.name;

}
owsley's kid
2009-05-01 12:00:55 UTC
Try it this way:



#include

#include

using std::string;

#include

using std::cout;

using std::cin;

using std::endl;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...