c++中函数和main必须合到一起才能编译通过?
发表于 : 2013-09-04 9:52
必须把两个文件合到一起才能编译通过?
//do.h
void splus(string &s)
{
s=s+"good!";
}
//main.cpp
#include <iostream>
#include <string>
#include "do.h"
using namespace std;
splus(string &s);
int main(int argc,char *argv[])
{
string ss="hello,";
splus(ss);
std::cout<<ss<<endl;
}
wei@wei-Lenovo-G475:~/program$ g++ -Wall -c main.cpp
In file included from main.cpp:3:0:
do.h:1:1: 错误: ‘string’不是一个类型名
main.cpp:5:17: 错误: expected constructor, destructor, or type conversion before ‘;’ token
main.cpp: 在函数‘int main(int, char**)’中:
main.cpp:9:13: 错误: ‘splus’在此作用域中尚未声明
wei@wei-Lenovo-G475:~/program$
后来,我改了,把两个文件合在一个main.cpp里面。
#include <iostream>
#include <string>
using namespace std;
void splus(string &s)
{
s=s+"good!";
}
int main(int argc,char *argv[])
{
string ss="hello,";
splus(ss);
std::cout<<ss<<endl;
}
再没有问题。为什么是这样?
//do.h
void splus(string &s)
{
s=s+"good!";
}
//main.cpp
#include <iostream>
#include <string>
#include "do.h"
using namespace std;
splus(string &s);
int main(int argc,char *argv[])
{
string ss="hello,";
splus(ss);
std::cout<<ss<<endl;
}
wei@wei-Lenovo-G475:~/program$ g++ -Wall -c main.cpp
In file included from main.cpp:3:0:
do.h:1:1: 错误: ‘string’不是一个类型名
main.cpp:5:17: 错误: expected constructor, destructor, or type conversion before ‘;’ token
main.cpp: 在函数‘int main(int, char**)’中:
main.cpp:9:13: 错误: ‘splus’在此作用域中尚未声明
wei@wei-Lenovo-G475:~/program$
后来,我改了,把两个文件合在一个main.cpp里面。
#include <iostream>
#include <string>
using namespace std;
void splus(string &s)
{
s=s+"good!";
}
int main(int argc,char *argv[])
{
string ss="hello,";
splus(ss);
std::cout<<ss<<endl;
}
再没有问题。为什么是这样?