Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 定义某些内容并在C++;_C++ - Fatal编程技术网

C++ 定义某些内容并在C++;

C++ 定义某些内容并在C++;,c++,C++,请原谅我的无知,我需要定义一些可以被另一个文件引用的东西(例如,一个函数),而不包括它的代码,就像Lua中的这样: --main.lua ;打印(需要“./func”); --func.lua ;返回函数() 打印“你好”; 结束; 这可能是一个问题的重复,但我没有发现它。。。当我编译代码时,编译器会让我看到这个错误 我的代码是: //////////////// main.cpp //////////////// #include "test.h" int main() { t

请原谅我的无知,我需要定义一些可以被另一个文件引用的东西(例如,一个函数),而不包括它的代码,就像Lua中的这样:

--main.lua
;打印(需要“./func”);
--func.lua
;返回函数()
打印“你好”;
结束;
这可能是一个问题的重复,但我没有发现它。。。当我编译代码时,编译器会让我看到这个错误

我的代码是:

//////////////// main.cpp ////////////////

#include "test.h"

int main() {
    test();
    return 0;
}

//////////////// test.h ////////////////

#ifndef INC_TEST
#define INC_TEST

void *test();

#endif  /* INC_TEST */


//////////////// test.cpp ////////////////

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

using namespace std;

void *test() {
    cout << "Hello World!" << endl;
}
///main.cpp////////////////
#包括“test.h”
int main(){
test();
返回0;
}
////////////////测试h////////////////
#ifndef公司测试
#定义INC_测试
void*test();
#endif/*INC_测试*/
////////////////test.cpp////////////////
#包括
#包括“test.h”
使用名称空间std;
void*test(){

目前,您的
test.cpp
尚未生成,因此链接器无法找到
void*test()
函数。请向编译器指定
test.cpp
以正确生成代码。使用
g++main.cpp test.cpp-o as3c.exe
使用
g++main.cpp test.cpp-o as3c.exe
@πῥεῖ 谢谢!它起作用了@πάνταῥεῖ 嗯……不管怎么说,我做得对吗?请告诉我,我不确定我做得对不对,真的。我在void之后需要那个
*
吗?“我在void之后需要那个*吗?”不。你不能从函数返回任何东西。@πάνταῥεῖ 我想这个符号会引用函数本身。好吧,我知道了,那么它只会使其返回值可引用?我想我知道了,只是还不能接受答案(7分钟)