简体   繁体   English

C++ 类没有成员

[英]C++ class has no member

I checked the suggested answer with a similar problem, added a default constructur but the problem remains:我用类似的问题检查了建议的答案,添加了一个默认构造函数,但问题仍然存在:

Auto.h:自动.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:和 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:在 Visual Studio 中,它强调(*)定义告诉我: 在此处输入图片说明

Is my thinking wrong that it is possible to define the setType function outside of the class?我的想法是否错误,可以在类之外定义setType函数?

It is not the default constructor, but you have a class declaration in your cpp file.它不是默认构造函数,但您的 cpp 文件中有一个类声明。

Auto.h自动.h

#include <string>

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

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

Auto.cpp自动cpp

#include "Auto.h"

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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