简体   繁体   English

.h 和.cpp 文件分离时出错,但仅使用 ah 文件时没有错误。 我究竟做错了什么?

[英]Error on .h and .cpp file separation, but no error on using just a .h file. What am I doing wrong?

I'm a C++ noob, so please go easy on me if my doubt seems too trivial.我是 C++ 菜鸟,所以如果我的疑问似乎太微不足道,请对我轻松一点。

#pragma once

class cube
{
public:
    double getVolume();
    double getSA();
    void setLength(double length);

private:
    double length_;
};

This is my header file cube.h ^^^这是我的 header 文件 cube.h ^^^

#include "cube.h"

double cube::getVolume()
{
    return length_ * length_ * length_;
}

double cube::getSA()
{
    return 6 * length_ * length_;
}

void cube::setLength(double length)
{
    length_ = length;
}

This is cube.cpp and the next is main.cpp这是 cube.cpp,接下来是 main.cpp

#include "cube.h"
#include <iostream>
int main()
{

    cube c;

    c.setLength(3.48);
    double vol = c.getVolume();
    std::cout << "Vol: " << vol;

    return 0;
}

Question 1: So when I include the implementation in.h file itself without using.cpp, the code runs as expected.问题1:所以当我将实现包含在.h文件本身而不使用.cpp时,代码按预期运行。 But when I separate the.h and.cpp files, I get an error saying: 'undefined reference to cube::setLength(double)', 'undefined reference to cube::getVolume()'' and 'exited with code=1 in 0.47 seconds'.但是,当我将 .h 和 .cpp 文件分开时,我收到一条错误消息:“未定义对cube::setLength(double)', 'undefined reference to cube::getVolume() 的引用”和“exited with code=1”在 0.47 秒内”。

I ran it on VS Code and just set it up, and just ran a simple cin/cout and a hello world program.我在 VS Code 上运行它并设置它,然后运行一个简单的 cin/cout 和一个 hello world 程序。 Is there something wrong I'm doing with the code itself or is it an error due to VS Code?我对代码本身做错了什么还是由于 VS Code 导致的错误? Do note that this problem occurs only when I separate the.h and.cpp files.请注意,仅当我将 .h 和 .cpp 文件分开时才会出现此问题。

Question 2: Why even separate the.h and.cpp files?问题2:为什么还要分开.h和.cpp文件呢?

Edit 1: As @KamilCuk pointed out, the problem might be in linking the main.cpp and cube.cpp files, but I have no clue how you 'link' the cpp files for compilation?编辑1:正如@KamilCuk 指出的那样,问题可能在于链接main.cpp 和cube.cpp 文件,但我不知道你如何“链接”cpp 文件进行编译?

Edit 2: I just might be compiling it the wrong way.编辑2:我可能只是以错误的方式编译它。 I usually just click the Code Runner enabled run button on the top right, but I have no clue how to compile two files at once.我通常只是单击右上角的启用 Code Runner 的运行按钮,但我不知道如何一次编译两个文件。

Try to do this.尝试这样做。 This worked for me and gave desired result:这对我有用并给出了预期的结果:

g++ main.cpp cube.cpp -I./ -o cube.out g++ main.cpp cube.cpp -I./ -o cube.out

You can implement in.h file inline as well and can be make your code work.您也可以实现 in.h 文件内联,并且可以使您的代码正常工作。 But for more complex classes better to separate the implementation in.cpp and.h但是对于更复杂的类最好将.cpp和.h中的实现分开

Also its a good practice to guard header file for multiple definitions:保护 header 文件的多个定义也是一个好习惯:

#ifndef _CUBE_H_
#define _CUBE_H_

class Cube
{
   public:
    double getVolume();
    double getSA();
    void setLength(double length);

   private:
    double length_;
 };

 #endif //_CUBE_H_

Below link provided details of how to build multiple C++ files using VSCode.下面的链接提供了如何使用 VSCode 构建多个 C++ 文件的详细信息。

https://dev.to/talhabalaj/setup-visual-studio-code-for-multi-file-c-projects-1jpi https://dev.to/talhabalaj/setup-visual-studio-code-for-multi-file-c-projects-1jpi

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

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