2014-12-07 82 views
0

我一直在这花费数小时,但我似乎无法找到解决这个问题的方法。 我有两个头文件,一个是Load.h,一个是Source.h。没有预定义的构造函数现有的C++

这是我load.h:

#ifndef LOAD_H 
#define LOAD_H 
#include <string> 
#include "Complexnumbersfrompreviousweek.h" 
#include "Otherfunctionsfrompreviousweek.h" 
#include "Source.h" 

    class Load : public Source //I'm doing this to inherit the vs term 
    { 
    private: 
     double load; 
     double vload; 
     double ApparentP; 

    public: 

     Load (double, double, double, double); 
     double Calcvload (double, double, double, double); 
    }; 
    #endif LOAD_H 

这是我Source.h:

#ifndef SOURCE_H 
#define SOURCE_H 
#include <string> 
#include "Complexnumbersfrompreviousweek.h" 
#include "Otherfunctionsfrompreviousweek.h" 

class Source { 
public: 
    double vs; 
    Source(double); 

    double Ret(double); 
}; 
#endif SOURCE_H 

这是我的第二个.cpp文件:

#include "Line.h" 
#include "Load.h" 
#include "Source.h" 
#include <fstream> 
#include <string> 
#include <sstream> 
#include <algorithm> 
#include <iostream> 
#include <math.h> 

using namespace std; 

Source::Source(double VoltageS) 
{ 
    VoltageS = vs; 
}; 
double Source::Ret(double vs) 
{ 
    return vs; 
} 
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor 
{ 
    Z = load; 
    Sl = ApparentP; 
    Vl = vload; 
    VoltageS = vs; 
}; 

错误我得到的错误是C2512:'Source'没有预定义的合适的构造函数可用。

这是我在我的main()现在做的:

Source Sorgente(VoltageS); 
Load loadimpedance(VoltageS, Sl, Z, Vl); 

所以基本上我创建使用电压作为参数(由用户选择的“Sorgente的”对象,我没有把那代码中),我试图将它分配给Vs,以便在构造函数中使用它之后加载...

在此先感谢您的帮助!

回答

2

由于LoadSource继承,它必须建立在其构造函数Source基地:

Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor 
{ 

既然你不明确指定一个,编译器会自动插入默认:

Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor 
: Source() // implicitly inserted by compiler 
{ 

但该构造函数不存在 - 因此错误。为了解决这个问题,你需要显式调用正确的构造函数:

Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor 
: Source(VoltageS) // explicitly construct the base 
{ 

Unrelatedly,在你Source构造您所指定的错误的元素:

Source::Source(double VoltageS) 
{ 
    VoltageS = vs; // you are assigning to the temporary instead of your member 
} 

这应该是:

Source::Source(double VoltageS) 
: vs(VoltageS) 
{ } 
+0

好的,非常感谢您的及时帮助。我明白你想说什么,但我没有明白你的意思:“你是分配给临时而不是你的成员”..我是不是将值VoltageS分配给Source类中的双变量?此外,我编译的程序,我现在得到:致命错误LNK1169 – pkpkpk 2014-12-07 21:05:11

+0

@Paolokiller不,你正在分配(未初始化)成员vs参数VoltageS。这是一个单独的问题 - 但可能你声明了一些函数,但没有定义它。 – Barry 2014-12-07 21:09:57

+0

我想我明白了,非常感谢! – pkpkpk 2014-12-07 21:14:49

相关问题