c++;,基类构造函数分离清除和实现 在我的C++程序中,我有单独的.h和.cp文件以及所有的东西都在工作,除非我想为派生类使用基类构造函数。它正在工作,但只有当我将函数定义放在类减速时,它才会工作。 以下是.h文件的工作代码 #include <iostream> using namespace std; class property { public: property(); property(const property & src); property(int src); ~property(); virtual int disp() const = 0; int get_ownable(); private: protected: int ownable; }; class rr : public property { public: rr(); rr(const rr & src); rr(int src):property(src) {cout << "\nderived class was called\n";}; ~rr(); virtual int disp() const; private: protected: };

c++;,基类构造函数分离清除和实现 在我的C++程序中,我有单独的.h和.cp文件以及所有的东西都在工作,除非我想为派生类使用基类构造函数。它正在工作,但只有当我将函数定义放在类减速时,它才会工作。 以下是.h文件的工作代码 #include <iostream> using namespace std; class property { public: property(); property(const property & src); property(int src); ~property(); virtual int disp() const = 0; int get_ownable(); private: protected: int ownable; }; class rr : public property { public: rr(); rr(const rr & src); rr(int src):property(src) {cout << "\nderived class was called\n";}; ~rr(); virtual int disp() const; private: protected: };,c++,class,inheritance,constructor,derived-class,C++,Class,Inheritance,Constructor,Derived Class,这样就可以了,但是如果我取消注释.imp文件中的函数并删除.h文件中的声明 rr(int src):property(src); 我得到了错误 head.h: IN constructor 'rr::rr(int)': head.h 113: error: expeted '{' at end of input imp.cpp: at global scope: imp.cpp:348:error: redefiniton of 'rr::rr(int); head.h.113: er

这样就可以了,但是如果我取消注释.imp文件中的函数并删除.h文件中的声明

    rr(int src):property(src);
我得到了错误

head.h: IN constructor 'rr::rr(int)':
head.h 113: error: expeted '{' at end of input
imp.cpp: at global scope:
imp.cpp:348:error: redefiniton of 'rr::rr(int);
head.h.113: error: 'rr::rr(int); previousle defined here
我在网上找到的所有示例都说明了如何使用类声明中定义的所有函数来实现这一点。我找不到任何关于如何使用2个文件的示例。有人能告诉我如何在一个单独的文件中定义基类构造函数调用吗? 我在使用g++编译器的Linux系统上。

像这样

基类.h

#pragma once

class BaseClass {
public:
    BaseClass(int a);

private:
    int a_private;
};
#pragma once

#include "BaseClass.h"

class Derived : public BaseClass {
public:
    Derived(int a);
private:
    int a_private;
};
BaseClass.cpp

#include "BaseClass.h"
#include <iostream>
using std::cout;
using std::endl;

BaseClass::BaseClass(int a) {
    cout << "Base class constructor called" << endl;
    this->a_private = a;
}
#include "Derived.h"
#include <iostream>
using std::cout;
using std::endl;

Derived::Derived(int a) : BaseClass(a) {
    cout << "Derived class constructor called" << endl;
    this->a_private = a;
}
#include "BaseClass.h"
#include "Derived.h"

int main() {
    Derived d(2);

    return 0;
}
派生的.cpp

#include "BaseClass.h"
#include <iostream>
using std::cout;
using std::endl;

BaseClass::BaseClass(int a) {
    cout << "Base class constructor called" << endl;
    this->a_private = a;
}
#include "Derived.h"
#include <iostream>
using std::cout;
using std::endl;

Derived::Derived(int a) : BaseClass(a) {
    cout << "Derived class constructor called" << endl;
    this->a_private = a;
}
#include "BaseClass.h"
#include "Derived.h"

int main() {
    Derived d(2);

    return 0;
}
使用命令编译
g++main.cpp-Derived.cpp-BaseClass.cpp
并运行将产生以下输出

Base class constructor called
Derived class constructor called

如评论(credits@IgorTandetnik)中所述,初始值设定项列表只应在实现文件中使用。不在头文件中(前提是类未模板化)

构造初始值设定项列表是实现的一部分,而不是声明。换句话说,它与构造函数体一起使用。您可以将两者都放在.h文件中,也可以将两者都放在.cpp文件中;你不能把它们分开。