多个头文件和类之间有什么关系?

发布于 2025-01-06 15:00:14 字数 1488 浏览 0 评论 0原文

我在这里有一个菜鸟问题。 我正在研究 C++ 结构和语法,但遇到了一些困难。 我知道我的概念中遗漏了一些东西。首先用一些代码来帮助描述情况。

Control.h

#pragma once
#ifndef CONTROL_H
#define CONTROL_H

class Control
{
    public:
        Control();
        ~Control();
    private:
    public:
};

#endif /*CONTROL_H*/

Control.cpp

#include "Control.h"
#include "Hello.h"

Hello helloObj;

Control::Control()
{
}

Control::~Control()
{
}

int main()
{
    int a = helloObj.HelloWorld();
    return 0;
}

Hello.h

#pragma once
#ifndef HELLO_H
#define HELLO_H

class Hello
{
    public:
        Hello();
        ~Hello();
    private:
    public:
         int HelloWorld(void);
};
#endif /*HELLO_H*/

Hello.cpp

#include "Hello.h"

Hello::Hello()
{
}

Hello::~Hello()
{
}

int HelloWorld()
{
    return 5;
}

我尝试在 OSX 10.7 上使用 g++ 编译 control.cpp 并得到

Undefined symbols for architecture x86_64:
      "Hello::Hello()", referenced from:
              __static_initialization_and_destruction_0(int, int)in cccZHWtd.o
      "Hello::~Hello()", referenced from:
              ___tcf_1 in cccZHWtd.o
      "Hello::HelloWorld()", referenced from:
              _main in cccZHWtd.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

这是编译器,我的代码还是我对正在发生的事情的概念? 我没有正确实例化某些东西吗?

任何更详细地描述这一点的链接将不胜感激。

最终我希望能够在另一个类中运行函数并返回结果...正常的面向对象,保持程序模块化...

I have a noob question here.
I'm getting my head around the C++ structure and syntax and I've hit a bit of a wall.
I know I am missing something from my concept. So first a little code to help describe the situation.

Control.h

#pragma once
#ifndef CONTROL_H
#define CONTROL_H

class Control
{
    public:
        Control();
        ~Control();
    private:
    public:
};

#endif /*CONTROL_H*/

Control.cpp

#include "Control.h"
#include "Hello.h"

Hello helloObj;

Control::Control()
{
}

Control::~Control()
{
}

int main()
{
    int a = helloObj.HelloWorld();
    return 0;
}

Hello.h

#pragma once
#ifndef HELLO_H
#define HELLO_H

class Hello
{
    public:
        Hello();
        ~Hello();
    private:
    public:
         int HelloWorld(void);
};
#endif /*HELLO_H*/

Hello.cpp

#include "Hello.h"

Hello::Hello()
{
}

Hello::~Hello()
{
}

int HelloWorld()
{
    return 5;
}

I try and compile control.cpp with g++ on OSX 10.7 and get

Undefined symbols for architecture x86_64:
      "Hello::Hello()", referenced from:
              __static_initialization_and_destruction_0(int, int)in cccZHWtd.o
      "Hello::~Hello()", referenced from:
              ___tcf_1 in cccZHWtd.o
      "Hello::HelloWorld()", referenced from:
              _main in cccZHWtd.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Is it the compiler, my code or my concept of whats going on?
Am I not instantiating something correctly?

Any links describing this in more detail would be appreciated.

Ultimately I want to be able to run a function in another class and return the result...normal OO, keeping your program modular stuff....

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

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

发布评论

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

评论(3

故事还在继续 2025-01-13 15:00:14

您收到的错误是链接错误,而不是编译错误。
链接器无法找到上述函数和函数的定义。因此它报告错误。您似乎尚未在项目中包含包含函数定义的 Hello.cpp 文件。

确保 Hello.cpp 包含在您的项目中并且是您项目的一部分,或者
如果您使用命令行进行编译和链接,请确保在命令行的文件名中指定了 Hello.cpp

The errors you are getting are Linking errors not compilation errors.
The linker is not able to find definitions of the said functions & hence it reports the errors. It seems You have not included the Hello.cpp file containing the function definitions in your project.

Make sure Hello.cpp is included in your project and is a part of your project or
If you are using command line for compilation and linking make sure you have specified Hello.cpp in the file names on the command line.

一抹微笑 2025-01-13 15:00:14

大多数问题是我对 g++ 不熟悉(谢谢 Als)。
也存在一些语法问题(感谢 Brain)。

这是更正后的代码(虽然对于结构的概述来说稍微有点臃肿)代码和 g++ 命令

Control.h

#pragma once
#ifndef CONTROL_H
#define CONTROL_H

class CONTROL
{
    private:
       //nothing defined yet...
    public:
        Control(); //default constructor
        ~Control(); //default destructor
};
#endif /*CONTROL_H*/

Control.cpp

#include "Hello.h"
#include "Control.h"

Hello helloTest; //instantiates the Hello Object

Control::Control()
{
}

Control::~Control()
{
}

int main()
{
    helloTest.HelloWorld();
    return 0;
}

Hello.h

#pragma once
#ifndef HELLO_H
#define HELLO_H

class Hello
{
    private:
        //nothing defined yet
    public:
        Hello(); //default constructor
        ~Hello(); //default destructor

        void HelloWorld();
};
#endif /*HELLO_H*/

Hello.cpp

#include "Hello.h"
#include <iostream> //so we can use 'cout'

using namespace std;

Hello::Hello()
{
}

Hello::~Hello()
{
}

void Hello::HelloWorld()
{
    std::cout << "Hello lovelies!\n"; //The magic word.
}

然后我们像这样运行 g++

g++ -o Hello ./Control.cpp ./Hello.cpp

g++ [选项] [输出文件名] [输入文件]

Most of the issue is me not being familiar as I should be with g++ (Thanks Als).
There were are few syntax issues as well (Thanks Brain).

Here is the corrected (albiet slightly bloated for an overview of stucture) code and g++ command

Control.h

#pragma once
#ifndef CONTROL_H
#define CONTROL_H

class CONTROL
{
    private:
       //nothing defined yet...
    public:
        Control(); //default constructor
        ~Control(); //default destructor
};
#endif /*CONTROL_H*/

Control.cpp

#include "Hello.h"
#include "Control.h"

Hello helloTest; //instantiates the Hello Object

Control::Control()
{
}

Control::~Control()
{
}

int main()
{
    helloTest.HelloWorld();
    return 0;
}

Hello.h

#pragma once
#ifndef HELLO_H
#define HELLO_H

class Hello
{
    private:
        //nothing defined yet
    public:
        Hello(); //default constructor
        ~Hello(); //default destructor

        void HelloWorld();
};
#endif /*HELLO_H*/

Hello.cpp

#include "Hello.h"
#include <iostream> //so we can use 'cout'

using namespace std;

Hello::Hello()
{
}

Hello::~Hello()
{
}

void Hello::HelloWorld()
{
    std::cout << "Hello lovelies!\n"; //The magic word.
}

Then we run g++ like so

g++ -o Hello ./Control.cpp ./Hello.cpp

g++ [option] [output file name] [input files]

泼猴你往哪里跑 2025-01-13 15:00:14

首先:

    public:
    Hello();
    ~Hello();
    private:
    public:

没有意义,一个类默认是private的,没必要弄成这样
公开两次,我也不知道你是否可以这样做,此外,如果你没有私人成员,私人不应该在那里(不是想刻薄,只是一些建议:-))

现在回答这个问题(带有猜测)免责声明:我是不是 100% 熟悉 GCC):

这是一个链接器错误,可能是因为
编译器找不到的定义
你好世界(无效);。
让我解释一下:

在你的头文件中你写了:

int HelloWorld(void);

但是在你的.cpp中你写了:

int HelloWorld()
{
   return 5;
}

函数的(或者在本例中是方法,因为它位于类内部)
标头和源中的参数需要完全相同,您
甚至不能更改名称(或者至少不能使用 VC++,这是
我用什么;我对 gcc 的经验很少)所以这可能是可以解决的
通过输入

int HelloWorld(void)
{
   return 5;
}

Next (免责声明,我不是 100% 熟悉预处理器):

您还使用 #pragma Once 预处理器标签,我不使用它,但是
我相信这意味着你只能包含一个文件一次,并且你已经包含了 Hello.h 和 Control.h 两次,就像我说的,我不是预处理器方面的专家,但你注释掉了

HELLO_H

CONTROL_H

First of all:

    public:
    Hello();
    ~Hello();
    private:
    public:

is pointless, a class defaults to private, and there is no need to make it
public twice nor do I know if you can do that furthermore if you have no private members private should not be in there (not trying to be mean just some advice :-) )

Now to answer the question (with a guess DISCLAIMER: I AM NOT 100% FAMILIAR WITH GCC):

This is a linker error, it may be there because
the compiler can not find the definition of
HelloWorld(void);.
Let me explain:

In your header file you wrote:

int HelloWorld(void);

However in your .cpp you write:

int HelloWorld()
{
   return 5;
}

The function's (or in this case method because it is inside a class)
arguments need to be exactly the same in the header and source, you
can not even change the names (or at least you cant with VC++ which is
what I use; I have little experience with gcc) so this may be resolvable
by typing

int HelloWorld(void)
{
   return 5;
}

Next (DISCLAIMER I AM NOT 100% familiar with the pre-proccsor):

You also use the #pragma once pre-proccsor tag, I dont use it but
I believe that means you can only include a file once and you have included Hello.h and Control.h twice, like I said I am no expert in the pre-proccsor but you commented out

HELLO_H

and

CONTROL_H

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