Compiler messed up, or wrong syntax...

Hi,

[code]
#include "stdafx.h"
#using
#include
#include
using namespace std;
#include
#include

class person
{
public:
char firstname[20];
char lastname[20];
int age;
double weight;
double height;

person( char firstname2[] = "", char lastname2[] = "", int age2 = 0, double weight2 = 0.0, double height2 = 0.0)
{
strcpy(firstname, firstname2);
strcpy(lastname, lastname2);
age = age2;
weight = weight2;
height = height2;
}
};



int _tmain(void)
{
char wholename[40];
int space;
person user("","",0,0.0,0.0);
cout<<"Enter your whole name: ";
getline(cin, user::firstname, ' '); //Gives an error
getline(cin, wholename, '');
space = wholename.find_first_of(' ');

for (int i = space; i >= space && i <=wholename.length; i++)
{
strcat(user::lastname, wholename[i]);
}

}
[/code]

It give me an error at the commented line//Gives an error. It gives this:

error C2510: 'user' : left of '::' must be a class/struct/union

I don't get it, I did declare user as a person class!! But it says that it isn't. Can anybody point out the mistake...

BattleGuard

[hr]
[size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]

[size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
:-D

Comments

  • : Hi,
    :
    : [code]
    : #include "stdafx.h"
    : #using
    : #include
    : #include
    : using namespace std;
    : #include
    : #include
    :
    : class person
    : {
    : public:
    : char firstname[20];
    : char lastname[20];
    : int age;
    : double weight;
    : double height;
    :
    : person( char firstname2[] = "", char lastname2[] = "", int age2 = 0, double weight2 = 0.0, double height2 = 0.0)
    : {
    : strcpy(firstname, firstname2);
    : strcpy(lastname, lastname2);
    : age = age2;
    : weight = weight2;
    : height = height2;
    : }
    : };
    :
    :
    :
    : int _tmain(void)
    : {
    : char wholename[40];
    : int space;
    : person user("","",0,0.0,0.0);
    : cout<<"Enter your whole name: ";
    [red]// since this is a instance variable, not a static variable, use . instead[/red]
    : getline(cin, user[red].[/red]firstname, ' '); //Gives an error
    : getline(cin, wholename, '');
    : space = wholename.find_first_of(' ');
    :
    : for (int i = space; i >= space && i <=wholename.length; i++)
    : {
    : strcat(user[red].[/red]lastname, wholename[i]);
    : }
    :
    : }
    : [/code]
    :
    : It give me an error at the commented line//Gives an error. It gives this:
    :
    : error C2510: 'user' : left of '::' must be a class/struct/union
    :
    : I don't get it, I did declare user as a person class!! But it says that it isn't. Can anybody point out the mistake...
    :
    : BattleGuard
    :
    : [hr]
    : [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]
    :
    : [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    : :-D
    :

  • : : Hi,
    : :
    : : [code]
    : : #include "stdafx.h"
    : : #using
    : : #include
    : : #include
    : : using namespace std;
    : : #include
    : : #include
    : :
    : : class person
    : : {
    : : public:
    : : char firstname[20];
    : : char lastname[20];
    : : int age;
    : : double weight;
    : : double height;
    : :
    : : person( char firstname2[] = "", char lastname2[] = "", int age2 = 0, double weight2 = 0.0, double height2 = 0.0)
    : : {
    : : strcpy(firstname, firstname2);
    : : strcpy(lastname, lastname2);
    : : age = age2;
    : : weight = weight2;
    : : height = height2;
    : : }
    : : };
    : :
    : :
    : :
    : : int _tmain(void)
    : : {
    : : char wholename[40];
    : : int space;
    : : person user("","",0,0.0,0.0);
    : : cout<<"Enter your whole name: ";
    : [red]// since this is a instance variable, not a static variable, use . instead[/red]
    : : getline(cin, user[red].[/red]firstname, ' '); //Gives an error
    : : getline(cin, wholename, '');
    : : space = wholename.find_first_of(' ');
    : :
    : : for (int i = space; i >= space && i <=wholename.length; i++)
    : : {
    : : strcat(user[red].[/red]lastname, wholename[i]);
    : : }
    : :
    : : }
    : : [/code]
    : :
    : : It give me an error at the commented line//Gives an error. It gives this:
    : :
    : : error C2510: 'user' : left of '::' must be a class/struct/union
    : :
    : : I don't get it, I did declare user as a person class!! But it says that it isn't. Can anybody point out the mistake...
    : :
    : : BattleGuard
    : :
    : : [hr]
    : : [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]
    : :
    : : [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    : : :-D
    : :
    :
    :


    Of course, I should've seen that, Thanx a bunch, Geez, I hate myself, but I have another problem(2 really):):

    In this line:

    getline(cin, user.firstname, ' ');

    Is it valid. Cin function, into the variable firstname, delimiter: ' ' a space.. Can't I do that. The compiler error is this:

    error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Ax> &' from 'char [20]'

    and:

    error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided

    and in the line:

    getline(cin, wholename);

    errors:

    error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided


    and:

    error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Ax> &' from 'char [40]'

    Can't you pass 2 or 3 arguments in a getline() function?? Thanx

    BattleGuard
    [hr]
    [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]

    [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    :-D

  • [b][red]This message was edited by tom_sw at 2003-6-30 11:7:18[/red][/b][hr]
    :
    : Of course, I should've seen that, Thanx a bunch, Geez, I hate myself, but I have another problem(2 really):):
    :
    : In this line:
    :
    : getline(cin, user.firstname, ' ');
    :
    : Is it valid. Cin function, into the variable firstname, delimiter: ' ' a space.. Can't I do that. The compiler error is this:
    :
    : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Ax> &' from 'char [20]'
    :
    : and:
    :
    : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
    :
    : and in the line:
    :
    : getline(cin, wholename);
    :
    : errors:
    :
    : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
    :
    :
    : and:
    :
    : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Ax> &' from 'char [40]'
    :
    : Can't you pass 2 or 3 arguments in a getline() function?? Thanx
    :
    : BattleGuard
    : [hr]
    : [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]
    :
    : [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    : :-D
    :
    :
    I haven't time to look into it fully now, but it looks like the type of 2nd arument to getline needs to be a string template, not a char array. Templates are not my strong point, but it looks like it doesn't detect that your char array is a string. Try going back and declaring the char arrays as strings and see if that helps.


  • : [b][red]This message was edited by tom_sw at 2003-6-30 11:7:18[/red][/b][hr]
    : :
    : : Of course, I should've seen that, Thanx a bunch, Geez, I hate myself, but I have another problem(2 really):):
    : :
    : : In this line:
    : :
    : : getline(cin, user.firstname, ' ');
    : :
    : : Is it valid. Cin function, into the variable firstname, delimiter: ' ' a space.. Can't I do that. The compiler error is this:
    : :
    : : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Ax> &' from 'char [20]'
    : :
    : : and:
    : :
    : : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
    : :
    : : and in the line:
    : :
    : : getline(cin, wholename);
    : :
    : : errors:
    : :
    : : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
    : :
    : :
    : : and:
    : :
    : : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Ax> &' from 'char [40]'
    : :
    : : Can't you pass 2 or 3 arguments in a getline() function?? Thanx
    : :
    : : BattleGuard
    : : [hr]
    : : [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]
    : :
    : : [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    : : :-D
    : :
    : :
    : I haven't time to look into it fully now, but it looks like the type of 2nd arument to getline needs to be a string template, not a char array. Templates are not my strong point, but it looks like it doesn't detect that your char array is a string. Try going back and declaring the char arrays as strings and see if that helps.
    :
    :
    :


    Ok, I reduced all of those errors to one and in this code:

    [code]
    //same class as above and

    //main functions:

    int _tmain(void)
    {
    string wholename;
    int space;
    person user("","",0,0.0,0.0);
    cout<<"Enter your whole name: ";
    getline(cin, wholename , ' ');
    getline(cin, wholename);
    space = wholename.find_first_of(' ');
    int namelen = wholename.length();
    for (int i = space; i >= space && i <= namelen; i++)
    {
    user.lastname += wholename[i]; // error line

    }

    }

    [/code]


    It gives me the error on the commented line:

    error C2106: '+=' : left operand must be l-value

    Thanx...

    BattleGuard

    [hr]
    [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]

    [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    :-D

  • : :
    : : getline(cin, user.firstname, ' ');
    : :

    I [b]Think[/b] that form of getline only takes a string argument
    [code]
    #include
    #include
    using namespace std;
    int main()
    {
    char buf[40];
    string name;

    cin.getline(buf,sizeof(buf),' '); // ok
    getline(cin,name); // ok
    getline(cin,buf,sizeof(buf), ' ') // NOT ok

    return 0;
    }
    [/code]

  • [b][red]This message was edited by stober at 2003-6-30 11:40:10[/red][/b][hr]
    [red]user.lastname is not a string class, so this is illegal[/red]
    [code]
    user.lastname += wholename[i]; // error line
    [/code]
    [red]suggestion[/red]
    [code]
    string sub = wholename.substr(0,space);
    strcpy(user.lastname,sub.c_str());
    [/code]




  • :
    : Ok, I reduced all of those errors to one and in this code:
    :
    : [code]
    : //same class as above and
    :
    : //main functions:
    :
    : int _tmain(void)
    : {
    : string wholename;
    : int space;
    : person user("","",0,0.0,0.0);
    : cout<<"Enter your whole name: ";
    : getline(cin, wholename , ' ');
    : getline(cin, wholename);
    : space = wholename.find_first_of(' ');
    : int namelen = wholename.length();
    : for (int i = space; i >= space && i <= namelen; i++)
    : {
    : user.lastname += wholename[i]; // error line
    :
    : }
    :
    : }
    :
    : [/code]
    :
    :
    : It gives me the error on the commented line:
    :
    : error C2106: '+=' : left operand must be l-value
    :
    : Thanx...
    :
    : BattleGuard
    :
    : [hr]
    : [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]
    :
    : [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    : :-D

    If it's still the same class, you are trying to add a string object with a pointer to a char array. You can't do that. If the class were changed to use strings instead of char arrays, that would work. Otherwise, you should be able to cast the string a char* and use strcpy to move the contents from the wholename string to the .lastname char array.

    These mixed string types are (and always have been) very confusing. The lack of strings is probably the single greatest ommision of the C language, and the various attempts to rectify that in C++ have added almost as many problems as they address (look into all the types of 'strings' that VC++ supports - CString, LPCTSTR, char arrays, b_str_t, B_STR, variant, and others!) However you address it in your code, try to use the same string types everywhere, it will make your life easier.
  • : [b][red]This message was edited by stober at 2003-6-30 11:40:10[/red][/b][hr]
    : [red]user.lastname is not a string class, so this is illegal[/red]
    : [code]
    : user.lastname += wholename[i]; // error line
    : [/code]
    : [red]suggestion[/red]
    : [code]
    : string sub = wholename.substr(0,space);
    : strcpy(user.lastname,sub.c_str());
    : [/code]
    :
    :
    :
    :
    :


    Thanx, that made that part work but how do I use a getline function like this:

    getline(cin, user.firstname , ' ');


    when user.firstname is a char array??? I think that the getline function only accepts strings, so how do I use a char array, and no, I can't declare the firstname as a string. Don't know why??

    Thanx

    BattleGuard

    [hr]
    [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]

    [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    :-D

  • [b][red]This message was edited by stober at 2003-6-30 12:9:8[/red][/b][hr]
    :
    : Thanx, that made that part work but how do I use a getline function like this:
    :
    : getline(cin, user.firstname ,sizeof(user.firstname), ' ');
    :
    [code]
    [red]cin.getline(user.firstname,' ');
    [blue]OR[/blue]
    cin >> user.firstname; // this stops at the first space[/red]
    [/code]
    :



  • : : :
    : : : getline(cin, user.firstname, ' ');
    : : :
    :
    : I [b]Think[/b] that form of getline only takes a string argument
    : [code]
    : #include
    : #include
    : using namespace std;
    : int main()
    : {
    : char buf[40];
    : string name;
    :
    : cin.getline(buf,sizeof(buf),' '); // ok
    : getline(cin,name); // ok
    : getline(cin,buf,sizeof(buf), ' ') // NOT ok
    :
    : return 0;
    : }
    : [/code]
    :
    :


    Hey, guys, I got everything to work. Answer to my last question:

    I used a temporary string. Here's the code:

    [code]
    #include "stdafx.h"

    #using
    #include
    #include
    using namespace std;
    #include
    #include

    class person
    {
    public:
    char firstname[20];
    char lastname[20];
    int age;
    double weight;
    double height;

    person( char firstname2[] = "", char lastname2[] = "", int age2 = 0, double weight2 = 0.0, double height2 = 0.0)
    {
    strcpy(firstname, firstname2);
    strcpy(lastname, lastname2);
    age = age2;
    weight = weight2;
    height = height2;
    }
    };


    // This is the entry point for this application
    int _tmain(void)
    {
    string wholename;
    string tempfn;
    int space;
    person user("","",0,0.0,0.0);
    cout<<"Enter your whole name: ";
    getline(cin, tempfn , ' ');
    getline(cin, wholename);
    for(int i = 0; i < tempfn.length(); i++)
    {
    user.firstname[i] = tempfn[i];
    }

    space = wholename.find_first_of(' ');
    int namelen = wholename.length();
    for (int i = space; i >= space && i <= namelen; i++)
    {
    string sub = wholename.substr(0,space);
    strcpy(user.lastname,sub.c_str());

    }
    cout<<"Hello, "
    <<user.firstname
    << " "
    <<user.lastname
    <<endl;
    }
    [/code]

    Man, if it took me that much time to debug just that peice of code, I wonder how much it will take for me to debug even little games!!! Heh, better off not thinking about it...

    BattleGuard
    [hr]
    [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]

    [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    :-D

  • All of your questions in this thread, except the first one, have a related problem: the difference between std::string and a char array. You need to understand where each of these can be used. In particular pay close attention when you see the word "string" in an error message but you are using a char array instead. Also, using operators with char arrays or char pointers will give unexpected results, as you saw with the += operator. When using char arrays, you should rely on the functions in the < cstring > header rather than operators and std::string member functions. Also, you need to use the correct input function with each type. In particular, std::getline() is used with std::string and cin.getline() is used with char arrays.

    HTH

  • :
    : Man, if it took me that much time to debug just that peice of code, I wonder how much it will take for me to debug even little games!!! Heh, better off not thinking about it...
    :

    The problems only become harder as you gain more experience.
  • : Man, if it took me that much time to debug just that peice of code, I wonder how much it will take for me to debug even little games!!! Heh, better off not thinking about it...
    :
    : BattleGuard

    Yup, games can get very complicated. It gives you some respect for John Carmack, Sid Meyer, and the like, eh?
  • : :
    : : Man, if it took me that much time to debug just that peice of code, I wonder how much it will take for me to debug even little games!!! Heh, better off not thinking about it...
    : :
    :
    : The problems only become harder as you gain more experience.
    :
    Ain't that the truth!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories