C++ 链接器错误类构造函数的多个定义
已解决 为了解决这个问题,我不得不删除旧的对象文件并重建整个项目。不幸的是,我不知道我犯这个错误的真正原因 可能是因为include语句放错了位置,或者accountsContainer.h的类声明中可能有定义,我同时删除了这些定义 我正在尝试编译一个小型C/C++应用程序,但遇到了一个链接问题: 确切的错误是:C++ 链接器错误类构造函数的多个定义,c++,linker,definition,C++,Linker,Definition,已解决 为了解决这个问题,我不得不删除旧的对象文件并重建整个项目。不幸的是,我不知道我犯这个错误的真正原因 可能是因为include语句放错了位置,或者accountsContainer.h的类声明中可能有定义,我同时删除了这些定义 我正在尝试编译一个小型C/C++应用程序,但遇到了一个链接问题: 确切的错误是: make all g++ -g -Wall -fmessage-length=0 -I "Z:/SRS/Verwaltung/EDV/Marcus EDV/Programmierun
make all
g++ -g -Wall -fmessage-length=0 -I "Z:/SRS/Verwaltung/EDV/Marcus EDV/Programmierung/link libraries/c++/curl-7.24.0/include/curl/" -c -o accountsContainer.o accountsContainer.cpp
g++ -o libcurl.exe signatureUpdater.o accountsContainer.o network.o registry.o emailAccount.o filesystem.o libcurl.o "Z:/SRS/Verwaltung/EDV/Marcus EDV/Programmierung/link libraries/c++/curl-7.24.0/lib/libcurl.dll"
accountsContainer.o: In function `accountsContainer':
G:\#1Arbeit\htdocs\libcurl/accountsContainer.cpp:11: multiple definition of `accountsContainer::accountsContainer()'
signatureUpdater.o:G:\#1Arbeit\htdocs\libcurl/accountsContainer.h:13: first defined here
collect2: ld returned 1 exit status
make: *** [libcurl.exe] Error 1
我不明白这个错误是从哪里来的。我已经在互联网上做了一些关于“错误的多重定义”的研究。
这就是我所知道的:
我认为可以根据需要多次声明类或函数,但我必须只定义一次
这就是我所做的:
我在accountsContainer.cpp中定义了类构造函数“accountsContainer::accountsContainer(){…}”,该构造函数从未包含在任何位置,但应仅与生成文件一起编译为accountsContainer.o
accountsContainer.h包含类的声明
但是为什么链接器会抱怨头文件中的accountsContainer::accountsContainer(){…}定义
这些是我的文件:
“accountsContainer.h”
我希望问题不是来自于糟糕的程序架构/设计
谢谢你的帮助 检查include-guards,也许你在另一个标题中使用了相同的名称。你重建了项目吗?你是说如果我做了清理并尝试重新编译整个项目?是的,我这样做了,但错误仍然存在。您确定signatureUpdater.o已被删除并重新生成吗?因为您的.h文件的第13行是#include,它与您的类没有任何关系。您是对的!我的rm.bat似乎包含错误。删除所有对象文件后,请手动正确链接。非常感谢。
/*
* accountsContainer.h
*
* Created on: 16.03.2012
* Author: Admin
*/
#ifndef ACCOUNTSCONTAINER_H_
#define ACCOUNTSCONTAINER_H_
#include "emailAccount.h"
#include <string>
#include <iostream>
#include <algorithm>
class accountsContainer {
public:
const static int MAX_ACCOUNTS = 50;
private:
emailAccount srsAccounts[],emailAccounts[];
//can only be initalized in the constructor
int nAccounts;
int nSrsAccounts;
//methods
private:
void emailToLowerCase();
void findSrsAccounts();
public:
accountsContainer();
void printSrsAccounts();
emailAccount & getSrsAccount(int);
};
#endif /* ACCOUNTSCONTAINER_H_ */
/*
* accountsContainer.cpp
*
* Created on: 16.03.2012
* Author: Admin
*/
#include "accountsContainer.h"
#include "signatureUpdater.h"
accountsContainer::accountsContainer() {
//init variables
nAccounts = 0;
nSrsAccounts = 0;
/*
* read email Accounts from registry and save them to the srsAccounts Array
*/
signatureUpdater::reg.getEmailAccounts(srsAccounts,MAX_ACCOUNTS);
//make all e-mail adresses lower case
emailToLowerCase();
findSrsAccounts();
}
void accountsContainer::printSrsAccounts(){
string locS;
for(int i=0;i < nSrsAccounts;i++){
wcout << L"Account " << i << L" : " << srsAccounts[i].displayName <<endl;
wcout << L"Name: " << srsAccounts[i].accName.c_str() << endl;
wcout << L"E-Mail:" << srsAccounts[i].email.c_str() << endl << endl;
}
}
emailAccount & accountsContainer::getSrsAccount(int i){
return srsAccounts[i];
}
void accountsContainer::emailToLowerCase(){
wstring s;
for(int i=0; i < nAccounts; i++){
s = emailAccounts[i].email;
std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int,int>(std::tolower));
emailAccounts[i].email = s;
}
}
void accountsContainer::findSrsAccounts(){
/*
* resets Number of SRS accounts
* then iterates all e-mail accounts
* and searches for domain srsonline.de
* in lowercase!
*/
size_t found;
wstring emailAdr;
nSrsAccounts = 0;
for(int i=0;i<nAccounts;i++){
emailAdr=emailAccounts[i].email;
found = emailAdr.rfind(L"srsonline.de");
if(found != string::npos && (emailAdr.length()-found) == 12){
/*
wcout << L"für E-mail Konto: " << emailAdr << endl;
cout << "srsonline.de found at: " << found << endl;
*/
// copy SRS Accounts to srsAccounts array
srsAccounts[nSrsAccounts] = emailAccounts[i];
nSrsAccounts++;
}
}
}
CXXFLAGS = -g -Wall -fmessage-length=0 -I "Z:/SRS/Verwaltung/EDV/Marcus EDV/Programmierung/link libraries/c++/curl-7.24.0/include/curl/" # -O2 no performance improvement because of debugging!
OBJS = signatureUpdater.o accountsContainer.o network.o registry.o emailAccount.o filesystem.o libcurl.o
LIBS = "Z:/SRS/Verwaltung/EDV/Marcus EDV/Programmierung/link libraries/c++/curl-7.24.0/lib/libcurl.dll"
TARGET = libcurl.exe
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)