2011-04-18 4 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가 올랐지 만 메인을 별도의 cpp 파일로 갖고 싶습니다.

나는 그것에 관해서는 첫 번째 cpp의 끝 부분에 태그를 붙이 겠지만 희망적으로는 그렇게하지 않을 것이다.

+3

Visual Studio IDE에 표시된대로 오류를 게시하십시오. 즉, 오류 메시지의 전체 목록입니다. – JackOfAllTrades

+0

코드를 강조 표시 한 후에 "{}"단추를 활성화하십시오. 이렇게하면 < 및 >이 사라지지 않습니다. –

+0

@Chai [형식] (http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) 코드를 올바르게 게시하십시오. 그리고 특히 이것과 같은 방대한 swathe를 게시 할 때! – razlebe

답변

2
은 CPP 파일 중 하나에

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

의 정의를 이동

.

링커가 마음에 들지 않습니다. 개인적으로 나도이 새로운 cpp 파일을 만들 것입니다 :)

+0

환상적! 도와 주셔서 대단히 감사합니다. 필자는 그것을 보아 왔지만 오래된 헤더이며, 너무 자세히 보지 않았습니다. 끔찍한 형식의 코드에 대해 사과드립니다.이 사이트의 새로운 내용입니다 : P 지나치게 많지 않으면 마지막 문제 하나를 도와 줄 수 있습니다. 새로운 주 cpp에서 파일, 다른 cpp 파일에 정의 된 함수를 호출 할 수 없습니다. 다시 말하지만, 나는 이것이 "학교 소년"문제라고 생각하지만, 어이; 나는 프로그래밍에 익숙하지 않고 모두가 어딘가에서 시작해야만 하는가? 도움을 주셔서 감사합니다. – Ryuu

+0

스크래치 그게 학교 소년 오류 롤 이었어. 방금 함수를 프로토 타입해야했습니다. 도와 주셔서 대단히 감사합니다. – Ryuu

2

헤더 파일에 독립 실행 형 함수 (본문 포함)를 정의 할 때는 인라인을 사용해야합니다 (기본적으로 클래스 정의 내에서 정의 된 함수는 인라인입니다).

예. 헤더 파일에 하나가되어야합니다

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

예 구조체와 LNK1169 & LNK2005를 repro 수하는

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

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

해결 방법 : 해당 CPP 파일에 초기화 구조체를 & 배열을 이동합니다.

+0

이것은 질문과 관련이없는 것으로 보입니다. (답변은 _7 년 전 _ 방법에 의해) –

+0

@ Lightness Races in Orbit : 아니요. 오류 만. 대신 별도의 Q & A가 필요합니까? –

관련 문제