【问题标题】:C++ Polymorphism / Class Dependency Error [closed]C ++多态性/类依赖错误[关闭]
【发布时间】:2013-12-24 16:11:45
【问题描述】:

我正在使用 C++ 创建一个具有多态性的基本系列。但是,我在编译时遇到错误,我不知道如何纠正它们(我说class dependency,因为在创建另一个之前,Parent 和 Child 都相互依赖):

错误 C2061:语法错误:标识符“父”

错误 C2143:语法错误:缺少 ';'在'*'之前

错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 没有 支持默认整数

错误 C2065:“父”:未声明的标识符

错误 C2065:“名称”:未声明的标识符

错误 C2614:'Child':非法成员初始化:'par' 不是 基地或成员

错误 C2065:'par':未声明的标识符

错误 C2227:'->addChild' 的左侧必须指向 类/结构/联合/泛型类型

错误 C2065:'par':未声明的标识符

错误 C2227:'->Name' 左侧必须指向类/结构/联合/通用 输入

错误 C2661: 'Child::Child' : 没有重载函数需要 2 个参数

错误 C2065:'par':未声明的标识符

错误 C2227:'->Name' 左侧必须指向类/结构/联合/通用 输入

/* Polymorphism Family */
#include <iostream>
#include <vector>
class Child{
public:
    Child(Parent* parent, char* name) : par(parent), Name(name){ par->addChild(this); };
    Parent* par;
    char* Name;
    virtual void Speak(void){
        std::cout << "I am a child, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
    }
};
class Parent{
public:
    Parent(char* name) : Name(name){};
    char* Name;
    std::vector<Child*> Children;
    virtual void Speak(void){
        std::cout << "I am a parent called " << Name << std::endl;
        if(Children.size() == 0){
            std::cout << "I have no children" << std::endl;
        }
        else{
            std::cout << "I have " << Children.size() << " children:" << std::endl;
            for(auto c : Children){
                std::cout << c->Name;
            }
        }
    }
    void addChild(Child* child){
        Children.push_back(child);
    }
};
class Father : public Parent{
public:
    Father(char* name) : Parent(name){}
    void Speak(void){
        std::cout << "I am a father called " << Name << std::endl;
        if(Children.size() == 0){
            std::cout << "I have no children" << std::endl;
        }
        else{
            std::cout << "I have " << Children.size() << " children:" << std::endl;
            for(Child* c : Children){
                std::cout << c->Name;
            }
        }
    }
};
class Son : public Child{
public:
    Son(Parent* parent, char* name) : Child(parent, name){}
    void Speak(void){
        std::cout << "I am a son, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
    }
};
int main(void){
    Father* Dad = new Father("James");
    Dad->Speak();
    Son* John = new Son(Dad, "John");
    John->Speak();
    Dad->Speak();
    delete[] Dad, John;
    getchar();
    return (0);
}

编辑:我现在有 2 个错误:

/* Polymorphism Family */
#include <iostream>
#include <vector>

class Parent;

class Child{
public:
    Child(Parent* parent, char* name);
    Parent* par;
    char* Name;
    virtual void Speak(void){
        std::cout << "I am a child, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
    }
};

class Parent{
public:
    Parent(char* name) : Name(name){};
    char* Name;
    std::vector<Child*> Children;
    virtual void Speak(void){
        std::cout << "I am a parent called " << Name << std::endl;
        if(Children.size() == 0){
            std::cout << "I have no children" << std::endl;
        }
        else{
            std::cout << "I have " << Children.size() << " child(ren):" << std::endl;
            for(auto c : Children){
                std::cout << c->Name << std::endl;
            }
        }
    }
    void addChild(Child* child){
        Children.push_back(child);
    }
};

Child::Child(Parent* parent, char* name){
    Child::par = parent;
    Child::Name = name;
    par->addChild(this); 
};

class Father : public Parent{
public:
    Father(char* name) : Parent(name){}
    void Speak(void){
        std::cout << "I am a father called " << Name << std::endl;
        if(Children.size() == 0){
            std::cout << "I have no children" << std::endl;
        }
        else{
            std::cout << "I have " << Children.size() << "child(ren):" << std::endl;
            for(auto c : Children){
                std::cout << c->Name << std::endl;
            }
        }
    }
};
class Son : public Child{
public:
    Son(Parent* parent, char* name) : Child(parent, name){}
    void Speak(void){
        std::cout << "I am a son, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
    }
};
int main(void){
    Father* Dad = new Father("James");
    Dad->Speak();
    Son* John = new Son(Dad, "John");
    John->Speak();
    Dad->Speak();
    delete[] Dad, John;
    getchar();
    return (0);
}

【问题讨论】:

  • 使用前向声明来打破循环依赖。另外,delete[] Dad, John 不会按照你的想法做
  • 你的代码很糟糕。见Modern C++The Definitive C++ Book Guide and List
  • 你的编译器应该在它的错误信息上输出行号。这是有充分理由的!
  • 嗨。 @user2976089 您是否通过在线编译器检查了此代码,以及我在答案的附加部分中提供的选项?

标签: c++ class polymorphism


【解决方案1】:

您的Child 类使用尚未定义的Parent 类(不在代码上方、Child 之前或某些包含的文件中)。要解决这个问题,只需在Child 之前(或“以上”)声明Parent 类。例如:

class Parent;

//child class declaration as posted in your code and other classes include Parent.
class Child{
public:
    Child(Parent* parent, char* name) : par(parent), Name(name){ par->addChild(this); };
    Parent* par;
...

希望这会有所帮助。

[添加] 这是作者询问的代码的工作示例:

    #include <iostream>
#include <vector>

using namespace std;

class Child;

class Parent {

public:
    Parent(const char* name) : Name(name){};
    const char* Name;
    std::vector<Child*> Children;

    void Speak(void);
    void addChild(Child* child);
};

class Child{
public:
    Child(Parent* parent, const char* name) {
        Child::par = parent;
        Child::Name = name;
        par->addChild(this); 
    }

    Parent* par;
    const char* Name;
    virtual void Speak(void){
        std::cout << "I am a child, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
    }
};

void Parent::Speak(void){
        std::cout << "I am a parent called " << Name << std::endl;
        if(Children.size() == 0){
            std::cout << "I have no children" << std::endl;
        }
        else{
            std::cout << "I have " << Children.size() << " child(ren):" << std::endl;
            for(auto c : Children){
                std::cout << c->Name << std::endl;
            }
        }
    }


void Parent::addChild(Child* child) {
        Children.push_back(child);
    }

class Father : public Parent{
public:
    Father(const char* name) : Parent(name){}
    void Speak(void){
        std::cout << "I am a father called " << Name << std::endl;
        if(Children.size() == 0){
            std::cout << "I have no children" << std::endl;
        }
        else{
            std::cout << "I have " << Children.size() << "child(ren):" << std::endl;
            for(auto c : Children){
                std::cout << c->Name << std::endl;
            }
        }
    }
};

class Son : public Child{
public:
    Son(Parent* parent, const char* name) : Child(parent, name){}
    void Speak(void){
        std::cout << "I am a son, my name is " << Name << " and my parent's name is " << par->Name << std::endl;
    }
};

int main(int argc, char** argv){

    //i suggest you to use stack-located variables against heap-located
    Father Dad("James");
    Dad.Speak();

    Son John(&Dad, "John");
    John.Speak();
    Dad.Speak();    

    // Father* Dad = new Father("James");
    // Dad->Speak();
    // Son* John = new Son(Dad, "John");
    // John->Speak();
    // Dad->Speak();
    // delete Dad; 
    // delete John;
    //getchar();
    return (0);
}

online cpp editor输出

Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1

Executing the program....
$demo 
I am a father called James
I have no children
I am a son, my name is John and my parent's name is James
I am a father called James
I have 1child(ren):
John

【讨论】:

  • 我仍然得到 4 个错误
  • 请发布错误。
  • Use of undefined parent 两次,left of -&gt;addChild must point to a class/struct/union/generic typeleft of -&gt; name must point to a...
  • 在实现Parent 类之后,您应该实现Child 类构造函数(调用Parent)。例如。 class Parent; class Child { Child(); ... } class Parent { ... } ... //"below" "Parent" class definition public Child::Child(Parent* parent, char* name) { /* invoke parent's method here */ }。希望这会有所帮助。
  • 我已经编辑了它,但我仍然有两个错误 - Use of undefined parentleft of -&gt; name must point to a...
猜你喜欢
  • 2021-10-04
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多