0

My Source Code:

#include <iostream>
#include <string>

int main(void) {

    struct character{

        std::string name;
        unsigned short int age;
        unsigned short int height;
        unsigned short int weight;
    }; 

    std::cout << "==============================================\n";
    std::cout << "Welcome to Jake's Character Console! (JCC v1.0)\n";
    std::cout << "==============================================\n\n";

    std::cout << "Let's start by describing your character..." << std::endl <<     std::endl;

    std::cout << "What is your character's name? ";

    std::cin >> character.name;        \\ <======= ERROR HERE ========

    std::cout << "Let's start by describing your character..." << std::endl << std::endl;

    std::cout << "Let's start by describing your character..." << std::endl << std::endl;

    std::cin.get();
    std::cin.get();

    return 0;
}

The Problem:

The error occurs at the 'std::cin >> character.name;' statement. I am a complete, absolute novice at C++. I was making this program to learn the ropes of data structures, but I ran across this error. How could I simply rewrite this code so that I could input data into the character.name member? Also, any expert advice would be much appreciated; I don't have much C++ prior knowledge. Thank you SOF community.

The Error:

'A non-static member reference must be relative to a specified object.'

beakr
  • 5,709
  • 11
  • 42
  • 66
Jake2k13
  • 231
  • 3
  • 8
  • 23
  • 2
    What errors are you getting? – David G Dec 27 '13 at 03:37
  • You need to make an **instance** of `character`. `character` is the name of the class, not an object. – Jesse Good Dec 27 '13 at 03:38
  • This isn't a class though? @JesseGood – Jake2k13 Dec 27 '13 at 03:39
  • @Jake2k13: You need a book on the fundmentals, a `class` in C++ is defined using the `class` or `struct` keywords. – Jesse Good Dec 27 '13 at 03:42
  • I really do, I just don't have much access to the proper materials where I am. What book is recommended for a C++ (or programming) beginner? @JesseGood – Jake2k13 Dec 27 '13 at 03:44
  • 1
    See http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list, `C++ Primer` is probably the best for starting out. – Jesse Good Dec 27 '13 at 03:47
  • 1
    @Jake2k13 [This book](http://www.amazon.ca/Sams-Teach-Yourself-Hour-Edition/dp/0672335670) is great for learning simple C++. The creator of C++ has a good book on C++ too [here](http://www.stroustrup.com/3rd.html). **Tip**: you should probably learn some C before C++, since C++ is based on C and some of their language features differ. I learned C first [here](http://cocoadevcentral.com/articles/000081.php) (*great* if you're on a UNIX system like Linux or Mac). – beakr Dec 27 '13 at 03:51

3 Answers3

3

character is the name of the class (i.e it is a type). What you're doing is equivalent to cin >> int.name which makes no sense because int is a type and a keyword, not an object.

Note: Not to say that character is an integer. Those are two different types in and of themselves. Another difference is that character is a class type and int is a built-in non-class type.

You need to create an object of type character which you can use. You do that by doing:

character myChar;

Then later on you do:

std::cin >> myChar.name;
David G
  • 94,763
  • 41
  • 167
  • 253
3

You need to declare an instance of the struct character:

character c;
c.name = "Cake";
std::cout << c.name << std::endl;
//=> "Cake"
beakr
  • 5,709
  • 11
  • 42
  • 66
0

You should likely be declaring a struct outside of the main function. However, you can create an instance of the class like this:

struct character{
    std::string name;
    unsigned short int age;
    unsigned short int height;
    unsigned short int weight;
}character; 

If you are going to do this, I would change the names around. This code is legal but may be confusing. This syntax seems to be used more in C.

r-s
  • 1,035
  • 2
  • 11
  • 21