【问题标题】:Undeclared identifier in class implementation file类实现文件中未声明的标识符
【发布时间】:2013-08-18 07:29:44
【问题描述】:

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:

#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" ;
};

我得到了这个错误,虽然:

错误 11 错误 C2065:“名称”:未声明的标识符 c:\users\work\documents\visual studio 2012\projects\book\firstclass\firstclass\plorg.cpp 7 1 firstclass

那么,我该怎么办?

【问题讨论】:

  • 您应该使用构造函数初始化程序列表。我也不认为这是你的第一个错误。
  • 您在“Plorg.h”中的成员都缺少std:: 命名空间限定符。不要将using namespace std; 放在标题中。修复成员和构造函数参数变量声明。

标签: c++ class implementation


【解决方案1】:

string 真的是std::string:

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

顺便说一句,您应该支持构造函数初始化列表,并且不要将默认参数值放在实现中:

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

【讨论】:

  • 谢谢,我仍然觉得像你这样的人有时间回答像我这样的人的问题真是太棒了。你是金子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 1970-01-01
相关资源
最近更新 更多