C++, good old LNK1169 (and LNK2005) errors(C++,旧的 LNK1169(和 LNK2005)错误)
问题描述
我有 4 个文件、2 个标题和 2 个 cpp 文件.
I have 4 files, 2 headers and 2 cpp files.
头文件一:
#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.
Please input the resistance in ohms.
(If you do not want to add a resistor input 0)
"; // add in 0 functionality
cin >> res;
while(b){
cout << "Is the resistor connected in series or in parallel?
";
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.
";
}
}
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.
Please input the capacitance in ohms.
(If you do not want to add a capacitor input 0)
";
cin >> cap;
while(b){
cout << "Is the capacitor connected in series or in parallel?
";
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.
";
}
}
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.
Please input the inductance in henries.
(If you do not want to add an inductor input 0)
";
cin >> ind;
while (b){
cout << "Is the inductor connected in series or in parallel?
";
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.
";
}
}
cmp.push_back(new inductor(ind, a));
}
cpp文件二:
#include "AC Circuits.h"
int main(){
return 0;
}
当我将 main 移动到第一个 cpp 文件的末尾时没有问题,而当它在自己的 cpp 文件中时,LNK1169 和 LNK2005 出现,但我宁愿将 main 作为单独的 cpp 文件.
When I move the main to the end of the first cpp file there's no problems, whereas when it's in its own cpp file, LNK1169 and LNK2005 come up, but I'd rather have the main as a separate cpp file.
如果涉及到它,我会将它标记到第一个 cpp 的末尾,但希望它不会出现.
If it comes to it, I'll just tag it onto the end of the first cpp, but hopefully it won't come to that.
推荐答案
移动定义
std::ostream & operator<< (ostream &mm, const comp::complex &c)
在其中一个 CPP 文件中.
in one of the CPP files.
链接器不喜欢这样.我个人也会为此创建一个新的 cpp 文件:)
The linker doesn't like this. Personally I'd create a new cpp file for this one too :)
这篇关于C++,旧的 LNK1169(和 LNK2005)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++,旧的 LNK1169(和 LNK2005)错误
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01