简体   繁体   中英

Struct errors with C++?

so I am reading C++primer 6th edition and am up to structs, but when trying to make a test file to run I get the following error:

xubuntu@xubuntu:~/C/C$ make test
g++     test.cpp   -o test
test.cpp: In function ‘int main()’:
test.cpp:14:41: error: expected primary-expression before ‘.’ token
make: *** [test] Error 1
xubuntu@xubuntu:~/C/C$

The code:

#include <iostream>
#include <stdio.h>

struct test{
        char name[20];
        float age;
        float worth;
};

int main(){
        using namespace std;
        test chris = {"Chris", 22, 22};
        cout << "This is Chris's data:" << test.chris;
        return 0;
}

You may try doing it like this:-

cout << "This is Chris's name:" << chris.name;

test is the name of the struct and chris is the variable name.

test is the name of the struct, chris is the name of the variable, so you need to be referring to chris . And you'll need to reference each field individually to print it out. IE:

cout << "This is Chris's name:" << chris.name;
cout << "This is Chris's age:" << chris.age;

cout << "This is Chris's data:" << test.chris this is wrong.

It should be cout << "This is Chris's data:" << chris.name

The answer is clearly written : test.cpp:14:41: error: expected primary-expression before '.' token

replace

cout << "This is Chris's data:" << test.chris;

with

    cout << "This is Chris's data:" << chris.name << " " << chris.age;

You're calling test.chris .

chris is the name of your struct variable.

What you want to call is chris.name .

That's because test.chris doesn't exist.

You want to use the chris.name , chris.worth , chris.age directly.

If you want to use something like std::cout << chris you have to overload the << operator. For example:

std::ostream& operator<<(std::ostream &stream, const test &theTest) {
     stream << "Name: " << theTest.name << ", Worth: " << theTest.worth << ", Age: " << theTest.age;
     return stream;
}

Then you could use it like this:

    test chris = {"Chris", 22, 22};
    cout << "This is Chris's data:" << chris;

Since no one has yet mentioned it, if you still want to use

cout << "This is Chris's data:" << // TODO

consider overloading the output stream operator "<<". This will tell the compiler what to do when it encounters a test object as the right-operand of the output stream operator.

struct test{
    char name[20];
    float age;
    float worth;
};

ostream & operator<<(ostream &, const test &);       // prototype

ostream & operator<<(ostream & out, const test & t){ // implementation
    // Format the output however you like
    out << " " << t.name << " " << t.age << " " << t.worth;
    return out;
}

so that this line now works:

cout << "This is Chris's data:" << chris;

test.chris does not exist. If you want to output the whole struct, you need to do something like:

std::cout << "This is Chris's Data:  " << chris.name << ", " << chris.age << ", " << chris.worth << std::endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM