2011-04-18 41 views
1

我有4个文件,2个头文件和2个cpp文件。C++,良好的旧LNK1169(和LNK2005)错误

头文件之一:

#ifndef complex_2 
#define complex_2 
#include<iostream> 
#include<cmath> 

using namespace std; 

namespace comp 
{ 
    class complex{ 
    protected: 
     double re, im; 
    public: 
     complex(){re = im = 0;} 
     complex(double re_in, double im_in){ 
      re = re_in; 
      im = im_in; 
     } 
     ~complex(){} 

     void set_re(double re_in){ 
      re = re_in; 
     } 
     void set_im(double im_in){ 
      im = im_in; 
     } 
     double get_re() const{ 
      return re; 
     } 
     double get_im() const{ 
      return im; 
     } 

     complex comp_conj() const{ 
      complex temp; 
      temp.set_re(re); 
      temp.set_im(-im); 
      return temp; 
     } 

     complex operator + (const complex &c)const{ 
      complex temp(re + c.get_re(), im + c.get_im()); 
      return temp; 
     } 

     complex operator - (const complex &c)const{ 
      complex temp(re - c.get_re(), im - c.get_im()); 
      return temp; 
     } 

     complex operator * (const complex &c1)const{ 
      complex temp; 
      double a(re), b(im), c(c1.get_re()), d(c1.get_im()); 
      temp.set_re(a*c - b*d); 
      temp.set_im(b*c + a*d); 
      return temp; 
     } 

     complex operator/(const complex &c1)const{ 
      complex temp; 
      double a(re), b(im), c(c1.get_re()), d(c1.get_im()); 
      temp.set_re((a*c + b*d)/(pow(c,2) + pow(d,2))); 
      temp.set_im((b*d - a*d)/(pow(c,2) + pow(d,2))); 
      return temp; 
     } 

     double mod() const{ 
      return(sqrt(pow(re,2) + pow(im,2))); 
     } 

     double arg() const{ 
      return(atan(im/re)); 
     } 

     friend ostream & operator << (ostream &mm, const complex &c); 
    }; 
ostream & comp::operator<< (ostream &mm, const complex &c){ 
    if(c.get_im() >= 0){ 
     mm << "(" << c.get_re() << " + " << c.get_im() << "i)" << endl; 
    } 
    if(c.get_im() < 0){ 
     mm << "(" << c.get_re() << " - " << -(c.get_im()) << "i)" << endl; 
    } 
    return mm; 
} 
} 
#endif 

头文件2:

#ifndef AC_Circuits_Header 
#define AC_Circuits_Header 
#include "Complex and Definitions.h" 
#include<fstream> 
#include<vector> 
#include<string> 

using namespace std; 
using namespace comp; 

class component{ 
public: 
    virtual ~component(){} 
    virtual void set_f(double m){} 
    virtual double get_f() = 0; 

    virtual complex impedance() = 0; 
    virtual double reactance() = 0; 
    virtual double phase_diff() = 0; 
}; 

class resistor : public component{ 
protected: 
    double R; 
    bool para_or_series; 
public: 
    resistor(){R = para_or_series = 0;} 
    resistor(double r_in, bool a){ 
     R = r_in; 
     para_or_series = a; 
    } 
    ~resistor(){} 

    double get_R(){return R;} 
    void set_f(double m){} 
    double get_f(){return 0;} 

    complex impedance(){ 
     complex Z_R(R, 0); 
     return Z_R; 
    } 

    double reactance(){return 0;} 
    double phase_diff(){return 0;} 
}; 

class capacitor : public component{ 
protected: 
    double C, f; 
    bool para_or_serie; 
public: 
    capacitor(){C = para_or_serie = 0;} 
    capacitor(double c_in, bool a){ 
     C = c_in; 
     para_or_serie = a; 
    } 
    ~capacitor(){} 

    double get_C(){return C;} 
    void set_f(double freq){f = freq;} 
    double get_f(){return f;} 

    complex impedance(){ 
     complex Z_C(0, (pow((2 * 3.14 * f * C), -1))); 
     return Z_C; 
    } 

    double reactance(){ 
     return (-(pow((2 * 3.14 * f * C), -1))); 
    } 

    double phase_diff(){ 
     complex Z_C(0, (pow((2 * 3.14 * f * C), -1))); 
     return Z_C.arg(); 
    } 
}; 

class inductor : public component{ 
protected: 
    double L, f; 
    bool para_or_series; 
public: 
    inductor() : L(0), para_or_series(0) {} 
    inductor(double l_in, bool a) : L(l_in), para_or_series(a) {} 
    ~inductor(){} 

    double get_R(){return 0;} 
    double get_C(){return 0;} 
    void set_f(double b){f = b;} 
    double get_f(){return f;} 

    double reactance(){ 
     return (2 * 3.14 * f * L); 
    } 

    complex impedance(){ 
     complex Z_L(0, (2 * 3.14 * f * L)); 
     return Z_L; 
    } 

    double phase_diff(){ 
     complex Z_L(0, (2 * 3.14 * f * L)); 
     return Z_L.arg(); 
    } 
}; 

class circuit : public resistor, public capacitor, public inductor{ 
protected: 
    complex Z_tot; 
public: 
    circuit() : Z_tot(0,0) {} 
    circuit(bool a, resistor &R, capacitor &C, inductor &L){ 
     if(a == 1){ 
      Z_tot = R.impedance() + C.impedance() + L.impedance(); 
     } 
     if(a == 0){ 
      complex one(1,0); 
      Z_tot = one/(one/R.impedance() + one/C.impedance() + one/L.impedance()); 
     } 
    } 
    ~circuit(){} 

    void set_f(){} 
    double get_f(){return 0;} 

    complex impedance(){} 
    double reactance(){return 0;} 
    double phase_diff(){return 0;} 
}; 
#endif 

CPP文件之一:

#include "AC Circuits.h" 

using namespace comp; 

vector<component *> cmp; 

void add_resistor(){ 
    double res; 
    string p_or_s; 
    bool a, b(1); 

    cout << "You have chosen to add a resistor.\nPlease input the resistance in ohms.\n (If you do not want to add a resistor input 0)\n"; // add in 0 functionality 
    cin >> res; 
    while(b){ 
     cout << "Is the resistor connected in series or in parallel?\n"; 
     cin >> p_or_s; 
     if(p_or_s == "Parallel" || p_or_s == "parallel" || p_or_s == "P" || p_or_s == "p"){ 
      a = 1; 
      b = 0; 
     } 
     if(p_or_s == "Series" || p_or_s == "series" || p_or_s == "S" || p_or_s == "s"){ 
      a = 0; 
      b = 0; 
     } 
     else{ 
      cerr << "ERROR: Your selection is invlaid, please input whether the resistor is connected in series or in parallel.\n"; 
     } 
    } 

    cmp.push_back(new resistor(res, a)); 
} 

void add_capacitor(){ 
    double cap; 
    string p_or_s; 
    bool a, b(1); 

    cout << "You have chosen to add a capacitor.\nPlease input the capacitance in ohms.\n (If you do not want to add a capacitor input 0)\n"; 
    cin >> cap; 
    while(b){ 
     cout << "Is the capacitor connected in series or in parallel?\n"; 
     cin >> p_or_s; 
     if(p_or_s == "Parallel" || p_or_s == "parallel" || p_or_s == "P" || p_or_s == "p"){ 
      a = 1; 
      b = 0; 
     } 
     if(p_or_s == "Series" || p_or_s == "series" || p_or_s == "S" || p_or_s == "s"){ 
      a = 0; 
      b = 0; 
     } 
     else{ 
      cerr << "ERROR: Your selection is invlaid, please input whether the capactior is connected in series or in parallel.\n"; 
     } 
    } 
    cmp.push_back(new capacitor(cap, a)); 
} 

void add_inductor(){ 
    double ind; 
    string p_or_s; 
    bool a, b(1); 

    cout << "You have chosen to add an inductor.\nPlease input the inductance in henries.\n (If you do not want to add an inductor input 0)\n"; 
    cin >> ind; 
    while (b){ 
     cout << "Is the inductor connected in series or in parallel?\n"; 
     cin >> p_or_s; 
     if(p_or_s == "Parallel" || p_or_s == "parallel" || p_or_s == "P" || p_or_s == "p"){ 
      a = 1; 
      b = 0; 
     } 
     if(p_or_s == "Series" || p_or_s == "series" || p_or_s == "S" || p_or_s == "s"){ 
      a = 0; 
      b = 0; 
     } 
     else{ 
      cerr << "ERROR: Your selection is invlaid, please input whether the resistor is connected in series or in parallel.\n"; 
     } 
    } 
    cmp.push_back(new inductor(ind, a)); 
} 

CPP文件中的两个:

#include "AC Circuits.h" 

int main(){ 
    return 0; 
} 

当我将主文件移动到第一个cpp文件的末尾时,没有任何问题,而当它位于它自己的cpp文件中时,LNK1169和LNK2005出现了,但我宁愿将main作为单独的cpp文件。

如果涉及到它,我只会将它标记到第一个cpp的末尾,但希望它不会出现这种情况。

+3

完全按照它们在Visual Studio IDE中显示的那样发布错误。即,错误消息的整个列表。 – JackOfAllTrades 2011-04-18 13:05:10

+0

确保在突出显示代码后激活“{}”按钮。这样,<和>不会消失。 – 2011-04-18 13:07:05

+0

@Chai当你的代码正确地发布到[格式化](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks)时请注意。特别是当发布这样一个巨大的swathe! – razlebe 2011-04-18 13:07:34

回答

2

移动的

std::ostream & operator<< (ostream &mm, const comp::complex &c) 

定义在CPP文件之一。

链接器不喜欢这样。就个人而言,我也会为这个创建一个新的cpp文件:)

+0

太棒了!非常感谢您的帮助,并且如此迅速。 我应该看到过,但这是我制作的一个老头,并且没有详细地查看它。 对于我的代码糟糕的格式,我很抱歉,我很明显是这个网站的新手:P 如果不是太多,也许你可以帮我解决最后一个问题: 在新的主要cpp文件,我似乎无法调用其他cpp文件中定义的函数。 同样,我觉得这是一个“学校男孩”的问题,但嘿,我对编程相当陌生,每个人都必须从某处开始吧? 非常感谢您的帮助。 – Ryuu 2011-04-19 21:44:56

+0

划痕,这是一个学校的男孩错误哈哈。我只需要对这些功能进行原型设计。非常感谢您的帮助 – Ryuu 2011-04-20 10:17:18

2

当您在头文件中定义独立函数(包含正文)时,必须使用内联(在类定义中定义的函数默认为内联)。

E.g.在头文件之一必须是:

inline ostream& operator<< (ostream &mm, const complex &c){ 
    if(c.get_im() >= 0){ 
     mm << "(" << c.get_re() << " + " << c.get_im() << "i)" << endl; 
    } 
    if(c.get_im() < 0){ 
     mm << "(" << c.get_re() << " - " << -(c.get_im()) << "i)" << endl; 
    } 
    return mm; 
} 

否则,你将定义在每个翻译单元,包括头这一功能,因此可以得到多个定义错误。

0

实施例至REPRO LNK1169 & LNK2005与结构: 放置在头文件中的以下内容:

Struct Foo 
    { 
    std::wstring Bar[3] 
    }; 

    Foo InitFoo[2] 
    { 
    {L"ConstBar1Val", L"ConstBar1Val2", L"ConstBar1Val3"}, 
    {L"ConstBar2Val", L"ConstBar2Val2", L"ConstBar3Val3"} 
    }; 

解决方案:移动初始化结构&阵列到相应的CPP文件。

+0

这似乎与这个问题没有多大关系(顺便提一下,这是在十七年前回答的) – 2018-02-26 11:53:54

+0

@Lightness在轨道上的比赛:不,它没有。只有错误。也许是一个单独的问答? – 2018-02-26 12:10:52