简体   繁体   English

C ++ Code :: Blocks不使用头文件进行编译

[英]C++ Code::Blocks not compiling with header files

Good evening! 晚上好! (morning?) (早上?)

I was wondering if anyone is familiar with the following issues. 我想知道是否有人熟悉以下问题。 There are three files here, which are Cat.cpp, Cat.h, and CatMain.cpp. 这里有三个文件,分别是Cat.cpp,Cat.h和CatMain.cpp。 The issues are as follows: 问题如下:

When I try to build Cat.cpp, I get the error "undefined reference to WinMain@16". 当我尝试构建Cat.cpp时,出现错误“对WinMain @ 16的未定义引用”。

When I try to build CatMain.cpp, I get undefined reference errors for the speak and jump functions. 当我尝试构建CatMain.cpp时,出现语音和跳转功能的未定义参考错误。

The files are in the same folder and the code is just one-liners: 这些文件位于同一文件夹中,而代码只是一线:

Cat.cpp 目录

#include <iostream>
#include "Cat.h"

using namespace std;

void speak()
{
    cout << "meow" << endl;
}

void jump()
{
    cout << "meow?" << endl;
}

Cat.h 类别

#ifndef CAT_H
#define CAT_H

void speak();
void jump();

#endif // CAT_H

CatMain.cpp CatMain.cpp

#include <iostream>
#include "Cat.h"

using namespace std;

int main()
{
    speak();
    jump();
    return 0;
}

Is there anything wrong with this code? 这段代码有什么问题吗? Or is anyone aware of whether or not this is a Code::Blocks or a compiler issue? 还是有人知道这是Code :: Blocks还是编译器问题?

Any help is greatly appreciated =) 任何帮助,不胜感激=)

All that your code does as of now is something like this : 到目前为止,您的代码所做的全部是这样的:

Cat.cpp :: Cat.cpp ::

#include <iostream>
using namespace std;
void speak();
void jump();
void speak()
{
    cout << "meow" << endl;
}

void jump()
{
    cout << "meow?" << endl;
}

CatMain.cpp :: CatMain.cpp ::

#include <iostream>

using namespace std;
void speak();
void jump();
int main()
{
    speak();
    jump();
    return 0;
}

You Cat.cpp has a main method missing because of which it wouldn't compile. Cat.cpp缺少一个main方法,因为它无法编译。

Your CatMain.cpp does not have any definition for speak() and jump() , hence undefined error. 您的CatMain.cpp没有对CatMain.cpp speak()jump()任何定义,因此未定义错误。

Point : CatMain.cpp doesn't know what Cat.cpp is trying to work out. 要点:CatMain.cpp不知道Cat.cpp试图解决什么问题。

int main() { return 0; }

added to Cat.cpp should let it compile. 添加到Cat.cpp的文件应该可以进行编译。

void speak(){
    cout << "defined" << endl;
}

void jump(){
    cout << "defined" << endl;
}

added to CatMain.app should work for it as well. 添加到CatMain.app的文件也应适用。

Your code is fine. 您的代码很好。 Most probably you don't want to compile separate files but project as whole. 最有可能您不想编译单独的文件,而是整个项目。 You should add both cpp to project in your IDE and linker will link them together resolving both issues. 您应该将两个cpp都添加到IDE中的项目中,链接器会将它们链接在一起,以解决这两个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM