0

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

所以我该怎么做?

4

1 回答 1

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) {}
于 2013-08-18T07:33:15.843 回答