简单的C++ 程序 编译不通过
test.h
#ifndef _TEST_H_
#define _TEST_H_
#include <vector>
typedef vector<int> VI;
typedef vector<VI> VII;
void input(VII &) ;
void print(const VI &) ;
bool findVec(VI & ,const VII &) ;
#endif
test.cpp
#include "test.h"
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
void input(VII & vii)
{
fstream in("a.txt");
int n,t;
in>>n;
vii.resize(n);
for (string s;n-- && getline(in,s) ;)
{
for (istringstream sin(s); sin>>t; )
{
vii[vii.size()-n-1].push_back(t) ;
}
}
}
void print(const VI & vi)
{
for (int i=0;i<vi.size();i++)
{
cout<<vi[i]<<" ";
}
cout<<endl;
}
bool findVec(VI & vi,VII & vii)
{
for (int i=0;i<vii.size();i++)
{
for (int j=0;j<vii[i].size();j++)
{
if (vii[i][j]==-1)
{
vi=vii[i];
return true;
}
}
}
return false;
}
main.cpp
#include "test.h"
int main()
{
VII matrix;
VI v;
input(matrix);
if (findVec(v,matrix))
{
print(v);
}
return 0;
}
报错
e:\test\testtt\test.h(6) : error C2143: syntax error : missing ';' before '<'
e:\test\testtt\test.h(6) : error C2143: syntax error : missing ';' before '<'
e:\test\testtt\test.h(7) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
e:\test\testtt\test.h(7) : error C2143: syntax error : missing ';' before '<'
e:\test\testtt\test.h(7) : error C2143: syntax error : missing ';' before '<'
e:\test\testtt\test.h(9) : error C2065: 'VII' : undeclared identifier
e:\test\testtt\test.h(9) : error C2059: syntax error : ')'
e:\test\testtt\test.h(10) : error C2143: syntax error : missing ',' before '&'
e:\test\testtt\test.h(10) : error C2059: syntax error : '&'
e:\test\testtt\test.h(11) : error C2065: 'VI' : undeclared identifier
e:\test\testtt\test.h(11) : error C2059: syntax error : ','
e:\test\testtt\main.cpp(7) : error C2146: syntax error : missing ';' before identifier 'matrix'
e:\test\testtt\main.cpp(7) : error C2065: 'matrix' : undeclared identifier
e:\test\testtt\main.cpp(8) : error C2146: syntax error : missing ';' before identifier 'v'
e:\test\testtt\main.cpp(8) : error C2065: 'v' : undeclared identifier
e:\test\testtt\main.cpp(9) : error C2065: 'input' : undeclared identifier
e:\test\testtt\main.cpp(10) : error C2065: 'findVec' : undeclared identifier
test.cpp
e:\test\testtt\test.h(6) : error C2143: syntax error : missing ';' before '<'
e:\test\testtt\test.h(6) : error C2143: syntax error : missing ';' before '<'
e:\test\testtt\test.h(7) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
e:\test\testtt\test.h(7) : error C2143: syntax error : missing ';' before '<'
e:\test\testtt\test.h(7) : error C2143: syntax error : missing ';' before '<'
e:\test\testtt\test.h(9) : error C2065: 'VII' : undeclared identifier
e:\test\testtt\test.h(9) : error C2059: syntax error : ')'
e:\test\testtt\test.h(10) : error C2143: syntax error : missing ',' before '&'
e:\test\testtt\test.h(10) : error C2059: syntax error : '&'
e:\test\testtt\test.h(11) : error C2065: 'VI' : undeclared identifier
e:\test\testtt\test.h(11) : error C2059: syntax error : ','
e:\test\testtt\test.cpp(10) : error C2065: 'vii' : undeclared identifier
e:\test\testtt\test.cpp(11) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
e:\test\testtt\test.cpp(25) : error C2143: syntax error : missing ',' before '&'
e:\test\testtt\test.cpp(25) : error C2059: syntax error : '&'
e:\test\testtt\test.cpp(27) : error C2065: 'vi' : undeclared identifier
e:\test\testtt\test.cpp(27) : error C2228: left of '.size' must have class/struct/union type
e:\test\testtt\test.cpp(29) : error C2065: 'cout' : undeclared identifier
e:\test\testtt\test.cpp(29) : error C2109: subscript requires array or pointer type
e:\test\testtt\test.cpp(29) : error C2297: '<<' : illegal, right operand has type 'char [2]'
e:\test\testtt\test.cpp(31) : error C2563: mismatch in formal parameter list
e:\test\testtt\test.cpp(31) : error C2568: '<<' : unable to resolve function overload
could be 'class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &__cdecl std::endl(class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &)'
d:\program files\microsoft visual studio\vc98\include\ostream(377) : see declaration of 'endl'
or 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl std::endl(class std::basic_ostream<char,struct std::char_traits<char> > &)'
d:\program files\microsoft visual studio\vc98\include\ostream(372) : see declaration of 'endl'
or 'class std::basic_ostream<_E,_Tr> &__cdecl std::endl(class std::basic_ostream<_E,_Tr> &)'
d:\program files\microsoft visual studio\vc98\include\ostream(367) : see declaration of 'endl'
e:\test\testtt\test.cpp(35) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
Error executing cl.exe.
testtt.exe - 38 error(s), 2 warning(s)
[解决办法]
加上一句
#include <vector>
using namespace std;
[解决办法]
那你就在每个地方都加上std:: 如std::vector