首页 > 解决方案 > 如何使用头文件声明在另一个 cpp 文件中定义的函数以在我的主文件中使用?

问题描述

我知道这可能以前已经回答过,但我已经尝试过答案,但它们不起作用。我有一个头文件(test declaration.h)和一个 cpp 文件(test.cpp 的定义),最后是一个主 cpp 文件(main.cpp)。当我编译并运行该项目时,我收到一个错误(C3861 'test': identifier not found)。所有文件都在同一个文件夹中。我正在使用 Visual Studio 2019。为什么会发生这种情况,我做错了什么?

编辑

我已将文件更改为没有空格,现在出现链接错误(LNK2019 unresolved external symbol "void __cdecl test(void)" (?test@@YAXXZ) referenced in function _main)

测试定义文件

#include <iostream>
#include "test declaration.h"
using namespace std; 

void test()
{
    cout << "hi";
}

测试decl.h

#pragma once

void test();

主文件

#include <iostream>
#include "test declaration.h"

int main()
{
    test();
}

标签: c++

解决方案


推荐阅读