简体   繁体   English

类实现文件中未声明的标识符

[英]Undeclared identifier in class implementation file

Plorg.h: Plorg.h:

#include <string>
    #ifndef PLORG_H
    #define PLORG_H

class Plorg{
private:
    string name;
    int CI;

public:
    Plorg();
    Plorg(const string & n,int x=50);
    ~Plorg();
    void ChangeID(int CIaux);
    void Report() const;
};  
#endif PLORG_H

Plorg.cpp: Plorg.cpp:

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

using namespace std;
Plorg::Plorg(){
    name="Plorga";
    CI=50;
};

Plorg::Plorg(const string & n,int CIaux=50){
    name=n;
    CI=CIaux;
};
void Plorg::ChangeID(int CIaux){
    CI=CIaux;
};

void Plorg::Report() const {
    cout << "My name is " << name << "!" <<endl;
    cout << "MY CI is" << CI << "." ;
};

Plorg::~Plorg(){
    cout << "Bye,human" ;
};

I get this error,thoguh: 我得到这个错误,thoguh:

Error 11 error C2065: 'name' : undeclared identifier c:\\users\\work\\documents\\visual studio 2012\\projects\\book\\firstclass\\firstclass\\plorg.cpp 7 1 firstclass 错误11错误C2065:'名称':未声明的标识符c:\\ users \\ work \\ documents \\ visual studio 2012 \\ projects \\ book \\ firstclass \\ firstclass \\ plorg.cpp 7 1 firstclass

So,what should I do? 所以我该怎么做?

string is really std::string : string真的是std::string

class Plorg{
private:
    std::string name;
//  ^^^
public:
    Plorg();
    Plorg(const std::string& n, int x=50);
//              ^^^

As an aside, you should favour the constructor initialization list, and don't put the default parameter value in the implementation: 顺便说一句,您应该支持构造函数初始化列表,并且不要在实现中放入默认参数值:

Plorg::Plorg(const std::string & n, int CIaux) : name(n), CI(CIAux) {}

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

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