栗粒粒 发表于 2020-4-9 00:25:27

关于codeblocks中头文件位置的疑问

请问用codeblocks时自定义的头文件(.h)和有关头文件里函数的定义一(.cpp)应该放在什么位置才能让main.cpp调用到这个头文件和头文件的定义呢?
现在是把这一个.h文件和两个.cpp文件随便放在了桌面上的一个新建文件夹里。

这是头文件
#ifndef COMPLEX_H_INCLUDED
#define COMPLEX_H_INCLUDED
class Complex
{
public:
    explicit Complex(double = 0.0, double = 0.0);
    Complex operator+(const Complex&) const;
    Complex operator-(const Complex&) const;
    void print() const;
private:
    double real;
    double imaginary;
};
#endif // COMPLEX_H_INCLUDED


这是头文件里函数的定义
#include<iostream>
#include"Complex.h"
using namespace std;
Complex::Complex(double realPart,double imaginaryPart)
:real(realPart),imaginary(imaginaryPart)
{

}
Complex Complex::operator+(const Complex &operand2) const
{
    return Complex(real+operand2.real,imaginary+operand2.imaginary);
}
Complex Complex::operator-(const Complex &operand2) const
{
    return Complex(real-operand2.real,imaginary-operand2.imaginary);
}
void Complex::print() const
{
    cout<<'('<<real<<","<<imaginary<<')';
}

运行时的报错:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\xuxia\Desktop\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\4.9.2\..\..\..\libmingw32.a(main.o):main.c:(.text.startup+0xa7)||undefined reference to `WinMain@16'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


这是main.cpp函数
#include<iostream>
#include"Complex.h"
using namespace std;
int main()
{
    Complex x;
    Complex y(4.3,8.2);
    Complex z(3.3,1.1);
    cout<<"x: ";
    x.print();
    cout<<"\ny: ";
    y.print();
    cout<<"\nz: ";
    z.print();

    x = y + z;
    cout<<"\n\nx = y + z:"<<endl;
    x.print();
    cout<<" = ";
    y.print();
    cout<<" + ";
    z.print();

    x = y - z;
    cout<<"\n\nx = y - z:"<<endl;
    x.print();
    cout<<" = ";
    y.print();
    cout<<" - ";
    z.print();
    cout<<endl;
}

这是运行时的报错:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::Complex(double, double)'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::Complex(double, double)'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::Complex(double, double)'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::operator+(Complex const&) const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::operator-(Complex const&) const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
C:\Users\xuxia\Desktop\大学课程\C++\10.16.o:10.16.cpp|| undefined reference to `Complex::print() const'|
||error: ld returned 1 exit status|
||=== Build failed: 15 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

人造人 发表于 2020-4-9 00:30:10

WinMain瞩目,应该创建控制台应用程序,而不是窗口程序
控制台应用程序的入口是main
窗口程序的入口是WinMain
页: [1]
查看完整版本: 关于codeblocks中头文件位置的疑问