编译器错误“collect2: Id returned 1 exit status”将类与 QObject 一起使用(带有 Qt Creator 的 QT 4.7)

发布于 2024-10-16 02:03:28 字数 3874 浏览 7 评论 0原文

我当前的项目中出现以下情况:编译器使用以下代码返回“collect2: Id returned 1 exit status”:

#ifndef BASE_02_H
#define BASE_02_H

#include <QtCore>

class Base_02
{

public:
    Base_02();

    virtual void method()=0;
};

#endif // BASE_02_H

#include "base_02.h"
#include <QtCore>

Base_02::Base_02()
{

}

//----------------------------------------------------------------------------------------------------------------------------------

#ifndef DERIVED_02_H
#define DERIVED_02_H

#include <QtCore>
#include "base_02.h"

class Derived_02 : public Base_02
{

public:
    Derived_02();
    void method();
};

#endif // DERIVED_02_H

#include "derived_02.h"
#include <QtCore>

Derived_02::Derived_02()
{

}

void Derived_02::method()
{
    qDebug() << "Derived_02::method()";
}

//----------------------------------------------------------------------------------------------------------------------------------

#ifndef BASE_H
#define BASE_H

#include <QtCore>

class Base : public QObject
{
    Q_OBJECT

public:
    Base(Base* p=NULL);

    virtual void method()=0;
};

#endif // BASE_H

#include "base.h"
#include <QtCore>

Base::Base(Base* p)
{

}

//----------------------------------------------------------------------------------------------------------------------------------

#ifndef DERIVED_H
#define DERIVED_H

#include <QtCore>
#include "base.h"

class Derived : public Base
{
    Q_OBJECT

public:
    Derived(Derived* p=NULL);
    void method();
};

#endif // DERIVED_H

#include "derived.h"
#include "derived_02.h"
#include <QtCore>

Derived::Derived(Derived* p)
{

}

void Derived::method()
{
    Derived_02 d;
    d.method();
}

//----------------------------------------------------------------------------------------------------------------------------------

#include <QtCore/QCoreApplication>
#include <QtCore>
#include "base.h"
#include "derived.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);



    Derived* derived = new Derived();



    return a.exec();
}

编译器显示以下内容:

... mingw32-make:进入目录 C:/Entwicklung SVN/debug test/dataSenderReceiverExample' C:/Qt/2010.04/mingw/bin/mingw32-make -f Makefile.Debug mingw32-make[1]:进入目录C:/Entwicklung SVN/debug test/dataSenderReceiverExample' g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I"......\Qt\2010.04\qt\include\QtCore" -I".... ..\Qt\2010.04\qt\include" -I"..\dataSenderReceiver" -I"......\Qt\2010.04\qt\include\ActiveQt" -I"调试" -I"... ...\Qt\2010.04\qt\mkspecs\win32-g++" -o debug\main.o main.cpp main.cpp:在函数“int main(int, char**)”中: main.cpp:14:警告:未使用的变量“派生” g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\dataSenderReceiverExample.exe debug/main.o -L"c:\Qt\2010.04\qt\lib" -L../dataSenderReceiver/debug -ldataSenderReceiver -lQtCored4 mingw32-make[1]:离开目录 C:/Entwicklung SVN/debug test/dataSenderReceiverExample' mingw32-make:离开目录C:/Entwicklung SVN/debug test/dataSenderReceiverExample' ../dataSenderReceiver/debug/libdataSenderReceiver.a(衍生.o):C:\Entwicklung SVN\debug test\dataSenderReceiver/衍生.cpp:14: 未定义引用 Derived_02::Derived_02()' ../dataSenderReceiver/debug/libdataSenderReceiver.a(衍生.o):C:\Entwicklung SVN\debug test\dataSenderReceiver/衍生.cpp:15: 未定义引用Derived_02::method()' Collect2: ld 返回 1 退出状态 mingw32-make[1]: * [debug\dataSenderReceiverExample.exe] 错误 1 mingw32-make: * [调试] 错误 2 Prozess“C:/Qt/2010.04/mingw/bin/mingw32-make.exe”将在 Rückgabewert %2 中运行。 Fehler beim Erstellen des Projekts dataSenderReceiverExample(Ziel:桌面) Beim Ausführen des Build-Schritts 'Make'

请帮忙!

I've got the following situation in my current project: "collect2: Id returned 1 exit status" returned by the compiler using the following code:

#ifndef BASE_02_H
#define BASE_02_H

#include <QtCore>

class Base_02
{

public:
    Base_02();

    virtual void method()=0;
};

#endif // BASE_02_H

#include "base_02.h"
#include <QtCore>

Base_02::Base_02()
{

}

//----------------------------------------------------------------------------------------------------------------------------------

#ifndef DERIVED_02_H
#define DERIVED_02_H

#include <QtCore>
#include "base_02.h"

class Derived_02 : public Base_02
{

public:
    Derived_02();
    void method();
};

#endif // DERIVED_02_H

#include "derived_02.h"
#include <QtCore>

Derived_02::Derived_02()
{

}

void Derived_02::method()
{
    qDebug() << "Derived_02::method()";
}

//----------------------------------------------------------------------------------------------------------------------------------

#ifndef BASE_H
#define BASE_H

#include <QtCore>

class Base : public QObject
{
    Q_OBJECT

public:
    Base(Base* p=NULL);

    virtual void method()=0;
};

#endif // BASE_H

#include "base.h"
#include <QtCore>

Base::Base(Base* p)
{

}

//----------------------------------------------------------------------------------------------------------------------------------

#ifndef DERIVED_H
#define DERIVED_H

#include <QtCore>
#include "base.h"

class Derived : public Base
{
    Q_OBJECT

public:
    Derived(Derived* p=NULL);
    void method();
};

#endif // DERIVED_H

#include "derived.h"
#include "derived_02.h"
#include <QtCore>

Derived::Derived(Derived* p)
{

}

void Derived::method()
{
    Derived_02 d;
    d.method();
}

//----------------------------------------------------------------------------------------------------------------------------------

#include <QtCore/QCoreApplication>
#include <QtCore>
#include "base.h"
#include "derived.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);



    Derived* derived = new Derived();



    return a.exec();
}

The compiler says the following:

...
mingw32-make: Entering directory C:/Entwicklung SVN/debug test/dataSenderReceiverExample'
C:/Qt/2010.04/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory
C:/Entwicklung SVN/debug test/dataSenderReceiverExample'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I"......\Qt\2010.04\qt\include\QtCore" -I"......\Qt\2010.04\qt\include" -I"..\dataSenderReceiver" -I"......\Qt\2010.04\qt\include\ActiveQt" -I"debug" -I"......\Qt\2010.04\qt\mkspecs\win32-g++" -o debug\main.o main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:14: warning: unused variable 'derived'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\dataSenderReceiverExample.exe debug/main.o -L"c:\Qt\2010.04\qt\lib" -L../dataSenderReceiver/debug -ldataSenderReceiver -lQtCored4
mingw32-make[1]: Leaving directory C:/Entwicklung SVN/debug test/dataSenderReceiverExample'
mingw32-make: Leaving directory
C:/Entwicklung SVN/debug test/dataSenderReceiverExample'
../dataSenderReceiver/debug/libdataSenderReceiver.a(derived.o):C:\Entwicklung SVN\debug test\dataSenderReceiver/derived.cpp:14: undefined reference to Derived_02::Derived_02()'
../dataSenderReceiver/debug/libdataSenderReceiver.a(derived.o):C:\Entwicklung SVN\debug test\dataSenderReceiver/derived.cpp:15: undefined reference to
Derived_02::method()'
collect2: ld returned 1 exit status
mingw32-make[1]: * [debug\dataSenderReceiverExample.exe] Error 1
mingw32-make: *
[debug] Error 2
The Prozess "C:/Qt/2010.04/mingw/bin/mingw32-make.exe" wurde mit dem Rückgabewert %2 beendet.
Fehler beim Erstellen des Projekts dataSenderReceiverExample (Ziel: Desktop)
Beim Ausführen des Build-Schritts 'Make'

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫多说 2024-10-23 02:03:28
#include "base_02.h"
#include <QtCore>

必须是:

#include <QtCore>
#include "base_02.h"

然后它就可以编译了!

#include "base_02.h"
#include <QtCore>

has to be:

#include <QtCore>
#include "base_02.h"

Then it compiles!

顾冷 2024-10-23 02:03:28

我在以下情况下遇到了这个问题:


A 是一个可执行(fi 控制台)项目并且
B 和 C 是库(fi static lib)项目并且
A 使用 B,B 使用 C。

那么

A 必须通过 .pro 文件了解 B 库和 C 库,并且
B 必须通过 .pro 文件了解 C 库。


假设

在 A .pro 文件中,库 C 在库 B 之前表示:

LIBS *= -L"../C/debug"
LIBS *= -lC
INCLUDEPATH *= "../C"

LIBS *= -L"../B/debug"
LIBS *= -lB
INCLUDEPATH *= "../B"

那么

就会出现上述错误。


解决方案:

在 A .pro 文件中将库 B 命名在库 C 之前:

LIBS *= -L"../B/debug"
LIBS *= -lB
INCLUDEPATH *= "../B"

LIBS *= -L"../C/debug"
LIBS *= -lC
INCLUDEPATH *= "../C"

I got this problem in the following situation:


Say

A is an executable (f.i. console) project and
B and C are library (f.i. static lib) projects and
A uses B and B uses C.

Then

A has to know B library and C library via .pro file and
B has to know C library via .pro file.


Say

in the A .pro file library C is denoted before library B:

LIBS *= -L"../C/debug"
LIBS *= -lC
INCLUDEPATH *= "../C"

LIBS *= -L"../B/debug"
LIBS *= -lB
INCLUDEPATH *= "../B"

Then

exactly the error described above accures.


Solution:

Name library B before library C in the A .pro file:

LIBS *= -L"../B/debug"
LIBS *= -lB
INCLUDEPATH *= "../B"

LIBS *= -L"../C/debug"
LIBS *= -lC
INCLUDEPATH *= "../C"
请帮我爱他 2024-10-23 02:03:28
I got same problem here...??
#include<iostream>
using namespace std;
class employee{
    protected:
        int id; 
        string name;
        int day;
        int salary;
    public:
        virtual void calsalary()=0;
};
class facultyemp:public employee{
    public:
        static int k;
        facultyemp(){
            k++;
            id=k;
        }
        void setname(){
            cout<<"Enter name:";
            cin>>name;
        }
        void setday(){
            cout<<"Enter worked days:";
            cin>>day;
        }
        void setsalary(){
            cout<<"Enter salary pr day:";
            cin>>salary;
        }
        void getname(){
            cout<<"Name: "<<name;
        }
        void getid(){
            cout<<"\nID: "<<id;
        }
        void calsalary(){
            cout<<"The net salary: ";
            cout<<day*salary;
        }

};
static int k=0;
int main(){
    employee *e;
    facultyemp f;
    e=&f;
    f.setname();
    f.setday();
    f.setsalary();
    f.getname();
    f.getid();
    e->calsalary();

}
I got same problem here...??
#include<iostream>
using namespace std;
class employee{
    protected:
        int id; 
        string name;
        int day;
        int salary;
    public:
        virtual void calsalary()=0;
};
class facultyemp:public employee{
    public:
        static int k;
        facultyemp(){
            k++;
            id=k;
        }
        void setname(){
            cout<<"Enter name:";
            cin>>name;
        }
        void setday(){
            cout<<"Enter worked days:";
            cin>>day;
        }
        void setsalary(){
            cout<<"Enter salary pr day:";
            cin>>salary;
        }
        void getname(){
            cout<<"Name: "<<name;
        }
        void getid(){
            cout<<"\nID: "<<id;
        }
        void calsalary(){
            cout<<"The net salary: ";
            cout<<day*salary;
        }

};
static int k=0;
int main(){
    employee *e;
    facultyemp f;
    e=&f;
    f.setname();
    f.setday();
    f.setsalary();
    f.getname();
    f.getid();
    e->calsalary();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文