0

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;
}
2
  • 7
    Please post the code in the question, not in an external link. Commented Sep 13, 2013 at 14:56
  • 1
    test.chris does not exist. test is the name of structure; chris is the instance of it. Commented Sep 13, 2013 at 14:58

8 Answers 8

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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;

Comments

1

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

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

Comments

1

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;

Comments

0

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;

Comments

0

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;

Comments

0

You're calling test.chris.

chris is the name of your struct variable.

What you want to call is chris.name.

3 Comments

"What you want to call is test.name" Should it not be more chris.name ?
test is the name of the struct. chris is the variable name. So it will be chris.name, not test.name.
Sorry yes, got them mixed up
-1

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.