简体   繁体   中英

C++ class has no member

I checked the suggested answer with a similar problem, added a default constructur but the problem remains:

Auto.h:

#include <string>

class Auto {
    unsigned int ps;
    std::string type;

public:
    Auto();
    void setPS(unsigned int ps);
    void setType(std::string type);
};

and Auto.cpp:

#include <string>
#include "Auto.h"

// (*)
void Auto::setType(std::string type)
{
}

class Auto {

public:
    
    void setPS(unsigned int ps) {
        ps = ps;
    }

};

In Visual Studio, it underlines the (*) definition telling me that: 在此处输入图片说明

Is my thinking wrong that it is possible to define the setType function outside of the class?

It is not the default constructor, but you have a class declaration in your cpp file.

Auto.h

#include <string>

class Auto 
{
    unsigned int ps;
    std::string type;

public:
    void setPS(unsigned int ps);
    void setType(std::string type);
};

Auto.cpp

#include "Auto.h"

void Auto::setType(std::string type)
{
    this->type = type;
}

void Auto::setPS(unsigned int ps)
{
    this->ps = ps;
}

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