[英]Why am I getting these 'already defined' linker errors?
我是C ++的初學者,但我有一些使用Java的經驗。 我收到一些我不明白的錯誤。 我附上了錯誤控制台的圖片和它下面的代碼。
Error 1 error LNK2005: "public: __thiscall VectorDouble::VectorDouble(void)" (??0VectorDouble@@QAE@XZ) already defined in Main.obj C:\Users\carrea\Code\Visual Studio\COMP201\Lab8_VectorDoubleClass\VectorDouble.obj Lab8_VectorDoubleClass
Error 2 error LNK2005: "public: __thiscall VectorDouble::VectorDouble(int)" (??0VectorDouble@@QAE@H@Z) already defined in Main.obj C:\Users\carrea\Code\Visual Studio\COMP201\Lab8_VectorDoubleClass\VectorDouble.obj Lab8_VectorDoubleClass
....
還有10個像這樣的錯誤
Error 13 error LNK1169: one or more multiply defined symbols found C:\Users\carrea\Code\Visual Studio\COMP201\Lab8_VectorDoubleClass\Debug\Lab8_VectorDoubleClass.exe 1 1 Lab8_VectorDoubleClass
Main.cpp的
#include "VectorDouble.cpp"
using namespace std;
void printVD(const VectorDouble& v);
int main()
{
VectorDouble p;
p.push_back(1);
p.push_back(4);
p.push_back(3);
VectorDouble v(p);
printVD(v);
printVD(p);
}
void printVD(const VectorDouble& v)
{
int n = v.size();
for(int i = 0; i<n; i++)
{
cout << v.getElementAt(n) << " ";
}
cout << endl;
}
VectorDouble.h
#pragma once
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <sstream>
using namespace std;
class VectorDouble
{
public:
VectorDouble(void);
~VectorDouble(void);
VectorDouble(int intSize);
// Copy constructor
VectorDouble(const VectorDouble& vd);
// = override
void operator =(const VectorDouble& RIGHT_SIDE);
private:
double *dArray;
int count, max_count;
public:
// returns number of occupied cells
int size(void) const;
// Returns total number of cells
int capacity(void) const;
// Adds an element to array
void push_back(double num);
// Resizes the array to be double the original max_count
void resize(void);
// Returns element at specified index
double getElementAt(int i) const;
// Requests that the capacity of the allocated storage space for the elements of the vector container be at least enough to hold n elements
void reserve(int n);
private:
// Sets every element to 0
void clear(void);
};
VectorDouble.cpp
#pragma once
#include "VectorDouble.h"
using namespace std;
VectorDouble::VectorDouble(void)
{
max_count = 100;
count = 0;
dArray = new double[max_count];
clear();
}
VectorDouble::VectorDouble(int intSize)
{
max_count = intSize;
dArray = new double[max_count];
clear();
}
VectorDouble::~VectorDouble(void)
{
cout << "vector with " << this->count << " is destroyed";
}
// Copy constructor
VectorDouble::VectorDouble(const VectorDouble& vd)
{
int mcVD = vd.capacity(), i=0;
max_count = mcVD;
dArray = new double[max_count];
clear();
while(i<max_count)
{
dArray[i] = vd.getElementAt(i);
i++;
}
}
// = override
void VectorDouble::operator =(const VectorDouble& RIGHT_SIDE)
{
int rightCount = RIGHT_SIDE.size(), i=0;
while(rightCount>max_count)
{
resize();
}
while(i<rightCount)
{
dArray[i] = RIGHT_SIDE.getElementAt(i);
i++;
}
count = i;
}
// returns number of occupied cells
int VectorDouble::size(void) const
{
return count;
}
// Returns total number of cells
int VectorDouble::capacity(void) const
{
return max_count;
}
// Adds an element to array
void VectorDouble::push_back(double num)
{
if(count==max_count)
{
resize();
}
dArray[count] = num;
count++;
}
// Resizes the array to be double the original max_count
void VectorDouble::resize(void)
{
double *p = new double[max_count*2];
for(int i = 0; i < count; i++)
{
p[i] = dArray[i];
}
dArray = p;
max_count*=2;
delete p;
}
// Returns element at specified index
double VectorDouble::getElementAt(int i) const
{
return dArray[i];
}
// Requests that the capacity of the allocated storage space for the elements of the vector container be at least enough to hold n elements
void VectorDouble::reserve(int n)
{
while(n<max_count)
resize();
}
// Sets every element to 0
void VectorDouble::clear(void)
{
for(int i = 0; i < max_count; i++)
dArray[i] = 0;
}
任何幫助將非常感激...
你應該在Main.cpp
包含"VectorDouble.h"
而不是"VectorDouble.cpp"
。
與許多其他語言相比,包含文件的整個概念在C ++中相當破碎。
首先,C ++將事物分為“聲明”和“定義”。 你可能只對程序中的某個東西有一個定義,但是你想要的聲明數量很多。 在您的VectorDouble.cpp
文件中,您正在定義事物,而在VectorDouble.h
文件中,您正在聲明事物。
C ++中的#include
指令簡單而愚蠢。 遇到這種情況時,編譯器會有效地進行簡單的文本替換。 #include
指令將替換為您包含的文件的內容。
當你#include
一個定義文件時,這意味着你可以在那里完成#include
有效定義。 這就是為什么你不應該包括"VectorDouble.cpp"
。 由於您可能還將該文件編譯為單獨的文件,因此您最終會得到至少兩個所有定義的副本。
在談論某些事情時,整個宣言與定義二分法變得非常混亂。 例如,如果一個函數被inline
聲明,則函數體不再被認為是一個definition
。 這意味着您可以根據需要使用inline
聲明的函數體的副本數量。 所需要的只是所有定義都相同。
同樣,即使包含函數體,聲明模板函數也是一個聲明。 這是因為聲明導致沒有生成代碼,只有模板實例化導致代碼生成。 這就是決定某件事是宣言還是定義的真正試金石。 如果它導致分配空間或者正確生成實際代碼,那么它就是一個定義,否則就是一個聲明。
您收到的錯誤是鏈接器錯誤,告訴您編譯器正在為某些成員函數找到多個定義。 如果你看一下這個錯誤信息的大塊:
public: __thiscall VectorDouble::VectorDouble(void)" (??0VectorDouble@@QAE@XZ) already defined in Main.obj
你可以看到埋藏在那里的事實,它正在談論已經定義的構造函數VectorDouble::VectorDouble()
。
我認為你遇到的特殊問題是在main.cpp的這一行:
#include "VectorDouble.cpp"
問題是這包括源文件,而不是頭文件。 因此,當您編譯main.cpp時,您將編譯所有main,以及VectorDouble.cpp中的所有定義。 當鏈接器嘗試將它與編譯VectorDouble.cpp時生成的目標文件鏈接起來時,它將找到所有內容的兩個定義 - 一個來自VectorDouble.cpp,另一個來自main.cpp。
要解決此問題,請將此行更改為讀取
#include "VectorDouble.h"
這應該可以解決您的問題。 但更一般地說,實際上#include
一個.cpp文件是非常罕見的。 您幾乎總是包含標題,而不是來源。
希望這可以幫助!
Main.cpp不應該是#include
-ing VectorDouble.cpp; 它應該#include
VectorDouble.h。 如果要鏈接兩個.cpp文件,鏈接器會在VectorDouble.cpp中看到所有內容(一次一個,一次是來自Main.cpp的#include
-d)。
當我嘗試在名為PresentDataStruct.h的單獨文件中定義類並創建PresentDataStruct.cpp文件時,我收到類似的鏈接錯誤。
以下是PresentDataStruct.h的內容:
#pragma once
#include <string>
using namespace System;
ref class CPresentDataStruct
{
public:
CPresentDataStruct();
~CPresentDataStruct();
public:
void SomeActionOnData();
public:
System::String^ Name;
DateTime^ BirthDay;
};
void CPresentDataStruct::SomeActionOnData()
{
//TO DO here
}
CPresentDataStruct::CPresentDataStruct()
{
}
CPresentDataStruct::~CPresentDataStruct()
{
}
在PresentDataStruct.cpp中它有一行:
include "PresentDataStruct.h"
然后,當我向maindialog.h文件中添加include "PresentDataStruct.h"
,會出現鏈接錯誤。
所以我把它移到了PresentDataStruct.cpp文件,鏈接錯誤消失了:
void CPresentDataStruct::SomeActionOnData()
{
//TO DO here
}
CPresentDataStruct::CPresentDataStruct()
{
}
CPresentDataStruct::~CPresentDataStruct()
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.