2014-12-07 34 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

相關問題