【问题标题】:Linker error multiple definition of class constructor链接器错误类构造函数的多重定义
【发布时间】:2012-04-01 19:36:51
【问题描述】:

已解决

我不得不删除旧的目标文件并重建整个项目来解决问题。不幸的是,我不知道我收到此错误的真正原因。

可能是一些错误放置的包含语句,或者可能在我同时删除的 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)

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

【问题讨论】:

  • 你重建项目了吗?
  • 你的意思是如果我做了 make clean 并尝试再次编译整个项目?是的,我这样做了,但错误仍然存​​在。
  • 您确定 signatureUpdater.o 被删除并重建了吗?因为您的 .h 文件的第 13 行是 #include 这与您的课程毫无关系。
  • 你是对的!看来我的 rm.bat 包含错误。删除所有手动正确链接的目标文件后。谢谢!

标签: c++ linker definition


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多