简体   繁体   中英

Why does this code say I need a class/struct/union?

I've been programming in java and AS3 and C# for some time, and decided to give C++ a try... So, I decided to create a simple program to see how objects work here. I have two files:

Human.h

#pragma once
#include <string>
#include <iostream>
using namespace std;

class Human
{
    private:
        int _age;
        string _name;
        bool _gender;

    public:
        void setAge(int);
        void setName(string);
        int getAge();
        string getName();
        bool getGender();


    Human(int age, string name, bool gender)
    { 
        setAge(age);
        setName(name);
        _gender = gender;
    }

    ~Human()
    {
    }
};

int Human::getAge(){
    return _age;
}

string Human::getName(){
    return _name;
}

bool Human::getGender(){
    return _gender;
}

void Human::setAge(int val){

}

void Human::setName(string val){
    _name = val;
}

And Main.cpp

#include <iostream>
#include "Human.h"
#include <string>


using namespace std;
void main(void){


Human *me;
me = new Human(27,"Mr Miyagi",true);

cout << "My name is "+me.getName()+" and I am "+me.getAge()+" years old";

}

What I get is a red line under the "me" word, and an error C2228: left of '.getName' must have class/struct/union

me is a pointer to a Human - so you need to use ->

cout expects you to use the << operator.

main should not return void, but int.

always delete what you new

int main() {
  Human *me;
  me = new Human(27,"Kostas Loupasakis",true);
  cout << "My name is " << me->getName() << " and I am " << me->getAge() << " years old";
  delete me;
}

Alternatively, you can do without the pointer, and use . :

int main() {
  Human me(27,"Kostas Loupasakis",true);
  cout << "My name is " << me.getName() << " and I am " << me.getAge() << " years old";
}

The first variant above allocates a Human from the free store, called heap - similar to java. You need to explicitly delete in c++ though.

The second variant allocates a Human on stack - java can only do this with primitive types (int, float) and so on, c++ can do this with any object. In this case, me is automatically destructed when it goes out of scope, at the final '}' in main.

It should be -

cout << "My name is " << me->getName() << "and I am " << me->getAge() << " years old";

me is a pointer to an object. So, it's members should be accessed by operator -> .


Also, note that Human::setAge() function does nothing.

the object 'me' is actually a pointer, so therefore you will need to use an arrow instead of a dot.

me->getname();

will solve the error.

You have declared me as a pointer. Either remove the * on the declaration, or Use me->GetName() etc to use it.

= = = edit = = =

Since there is no default constructor, the * must remain. Either that or change it to

Human me(27, "name", true);

Others have addressed the direct problem you asked about. If you will indulge me, let me address a tangential issue.

Here is what you started with.

void main()
{
    // You could combine these two lines like this:
    //    Human *me = new Human(27,"Kostas Loupasakis",true);
    Human *me;
    me = new Human(27,"Kostas Loupasakis",true);

    cout << "My name is "+me->getName()+" and I am "+me->getAge()+" years old";
}

I can't speak to AS3, but in java and C#, you have the garbage collector to clean up allocated memory. There is no such beast in C++, so you have to clean up the memory yourself like this.

void main()
{
    Human *me = new Human(27,"Kostas Loupasakis",true);

    cout << "My name is "+me->getName()+" and I am "+me->getAge()+" years old";

    delete me;
}

The call to new allocates the Human object on the heap, and delete frees the memory when you are done with it. Here is a better way to do it.

void main()
{
    Human me(27,"Kostas Loupasakis",true);

    cout << "My name is "+me.getName()+" and I am "+me.getAge()+" years old";
}

In this case, space for the Human object is reserved on the stack as opposed to the heap. Thus, the space is automatically reclaimed when the function exits. Additionally, you can use the . notation instead of the -> notation since you are dealing with an object as opposed to a pointer.

I hope this helps.

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