链接器错误类构造函数的多个定义

时间:2012-03-16 08:50:47

标签: c++ linker definition

解决

我不得不删除旧的对象文件并重建整个项目来解决问题。不幸的是,我不知道为什么会出现这个错误。

也许它出现了一些错误的包含语句,或者在我同时删除的accountsContainer.h的类声明中可能有定义。

我正在尝试编译一个小的C / C ++应用程序,并且遇到了链接问题:

确切的错误是这样的:

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(){...}”,它永远不会包含在任何地方,但只能使用我的make-file编译到accountsContainer.o accountsContainer.h包含类

的声明

但为什么链接器会在头文件中抱怨accountsContainer :: accountsContainer(){...}的定义?

这些是我的档案: “accountsContainer.h”

/*
 * 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”

    /*
 * 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++;

        }
    }
}

那就是我的makefile:

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)

我希望问题不是来自糟糕的程序架构/设计 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

检查包含警卫,也许你在另一个标题中使用相同的名称。