【问题标题】:calling a function from a class inside a namespace?从命名空间内的类调用函数?
【发布时间】:2021-06-12 05:09:27
【问题描述】:

我编写了一个代码,其中包含一个存在于命名空间中的类。

这是头文件:如您所见,该类是在命名空间内创建的,在 frileg.h 中。

namespace frileg
{
    class legeplads
    {
    public:
        int x;
        int y;
        int z;
        void print();
    };
}

然后我还在legeplads.cpp中定义了函数print():

void frileg::legeplads::print()
{
    std::cout << "pos: {" << x << ", " << y << ", " << z << "}" << std::endl;
}

但是当我在 main 函数中运行代码时,我得到一个错误:“undefined reference to `frileg::legeplads::print()':”

int main(){

    legeplads a;
    a.x = 1;
    a.y = 2;
    a.z = 3;
    a.print();
    return 0;
}

【问题讨论】:

  • 您是否修改了 tasks.json 以使 Visual Studio Code 将多个源文件编译到您的可执行文件中。 VSCode 的默认行为是只构建活动文件,所以如果你有 2 个 cpp 文件并且你没有修改你的 tasks.json 预计会出现链接器错误。
  • 我该怎么做?
  • "command": "C:\\DEV\\MinGW\\bin\\g++.exe", "args": [ "-g3", "-Wall", "${file }", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" },
  • 应该编辑哪一部分?
  • VSCode 的文档在这里解释了如何使用多个源文件进行编译:https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson

标签: c++ class visual-studio-code namespaces


【解决方案1】:

我能够使用以下方法来完成这项工作。

  1. 在 VSCode 中创建一个新文件夹。我将其命名为SO_66629443_20210315 并将其放入我的 X:\test 文件夹中

  2. 添加 3 个文件。

  3. 修复缺少的包含。 legeplads.cpp 需要 #include &lt;iostream&gt;#include "frileg.h"main.cpp 需要 #include "frileg.h"

  4. 修改main.cpp,在构造a时使用frileg命名空间;使用frileg::legeplads a; 而不是legeplads a;

所以此时文件看起来像:

main.cpp

#include "frileg.h"

int main()
{

    frileg::legeplads a;
    a.x = 1;
    a.y = 2;
    a.z = 3;
    a.print();
    return 0;
}

legeplads.cpp

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

void frileg::legeplads::print()
{
    std::cout << "pos: {" << x << ", " << y << ", " << z << "}" << std::endl;
}

frileg.h

namespace frileg
{
    class legeplads
    {
    public:
        int x;
        int y;
        int z;
        void print();
    };
}
  1. 修改 tasks.json 以编译多个文件,正如文档在此处告诉您的那样:https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson

所以tasks.json 文件看起来完全像:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${workspaceFolder}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\msys64\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

请注意,我正在使用 msys2 进行 mingw 安装。我使用这些说明安装了它:https://www.msys2.org/

最后在 VSCode 中执行程序是这样的:

PS X:\test\SO_66629443_20210315>  & 'c:\Users\dresc\.vscode\extensions\ms-vscode.cpptools-1.2.2\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-gtfgey3y.olb' '--stdout=Microsoft-MIEngine-Out-l2sggqju.oz3' '--stderr=Microsoft-MIEngine-Error-zgahgwi3.zwy' '--pid=Microsoft-MIEngine-Pid-0mc3zqbv.bqm' '--dbgExe=C:\msys64\mingw64\bin\gdb.exe' '--interpreter=mi'    
pos: {1, 2, 3}

【讨论】:

  • 但我完全按照你的做法,代码可以编译,但不会执行。
  • 我想通了,我只是按错了按钮。
  • 按 F5 进行调试或按 ctrl-F5 无需调试即可运行。
猜你喜欢
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 2012-06-11
相关资源
最近更新 更多