注册 登录
编程论坛 C++教室

很简单的问题

chenjiang001 发布于 2007-01-03 12:42, 828 次点击

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

class Location()
{
public:
Location(int xx=0,int yy=0);
Location(Location & p);
~Location();
int GetX(){return X;}
int GetY(){return Y;}
private:
int X,Y;
};
Location::Location(int xx,int yy)
{
X=xx;
Y=yy;
cout<<"constructor Object.\n";
}
Location::~Location()
{
cout<<X<<","<<Y<<"objiect destroyed."<<endl;
}
Location::Location(Location & p)
{
X=p.X;
Y=p.Y;
cout<<"copy constructor called.\n";
}
void f(Location p)
{
cout<<"Function"<<p.GetX()<<","<<p.GetY()<<endl;
}
void main()
{
Location B(1,2);
f(B);
}

这个程序应该没问题吧.怎么通不过编译?
王高手指点!

8 回复
#2
一二三四五2007-01-03 12:51
using namespace std;
加上这句
#3
chenjiang0012007-01-03 13:04

error C2871: 'std' : does not exist or is not a namespace
D:\chenjiang\\gouzao\gouzao.cpp(12) : error C2143: syntax error : missing ';' before 'public'
D:\chenjiang\\gouzao\gouzao.cpp(14) : error C2065: 'p' : undeclared identifier
D:\chenjiang\\gouzao\gouzao.cpp(14) : error C2296: '&' : illegal, left operand has type 'int (__cdecl *)(void)'
D:\chenjiang\\gouzao\gouzao.cpp(14) : error C2660: 'Location' : function does not take 1 parameters
D:\chenjiang\\gouzao\gouzao.cpp(15) : warning C4552: '~' : operator has no effect; expected operator with side-effect
D:\chenjiang\\gouzao\gouzao.cpp(16) : error C2601: 'GetX' : local function definitions are illegal
D:\chenjiang\\gouzao\gouzao.cpp(17) : error C2601: 'GetY' : local function definitions are illegal
D:\chenjiang\gouzao\gouzao.cpp(18) : error C2143: syntax error : missing ';' before 'private'
D:\chenjiang\\gouzao\gouzao.cpp(21) : error C2653: 'Location' : is not a class or namespace name
D:\chenjiang\\gouzao\gouzao.cpp(23) : error C2065: 'X' : undeclared identifier
D:\chenjiang\\gouzao\gouzao.cpp(24) : error C2065: 'Y' : undeclared identifier
D:\chenjiang\\gouzao\gouzao.cpp(26) : warning C4508: 'Location' : function should return a value; 'void' return type assumed
D:\chenjiang\\gouzao\gouzao.cpp(27) : error C2653: 'Location' : is not a class or namespace name
D:\chenjiang\\gouzao\gouzao.cpp(28) : error C2084: function 'int __cdecl Location(void)' already has a body
D:\chenjiang\\gouzao\gouzao.cpp(31) : error C2653: 'Location' : is not a class or namespace name
D:\chenjiang\\gouzao\gouzao.cpp(31) : error C2563: mismatch in formal parameter list
D:\chenjiang\\gouzao\gouzao.cpp(32) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
D:\chenjiang\\gouzao\gouzao.cpp(37) : error C2146: syntax error : missing ')' before identifier 'p'
D:\chenjiang\\gouzao\gouzao.cpp(37) : error C2182: 'f' : illegal use of type 'void'
D:\chenjiang\\gouzao\gouzao.cpp(37) : error C2440: 'initializing' : cannot convert from '' to 'int'
Context does not allow for disambiguation of overloaded function
D:\chenjiang\\gouzao\gouzao.cpp(37) : error C2059: syntax error : ')'
D:\chenjiang\\gouzao\gouzao.cpp(38) : error C2143: syntax error : missing ';' before '{'
D:\chenjiang\\gouzao\gouzao.cpp(38) : error C2447: missing function header (old-style formal list?)
D:\chenjiang\\gouzao\gouzao.cpp(43) : error C2146: syntax error : missing ';' before identifier 'B'
D:\chenjiang\\gouzao\gouzao.cpp(43) : warning C4551: function call missing argument list
D:\chenjiang\\gouzao\gouzao.cpp(43) : error C2065: 'B' : undeclared identifier
Error executing cl.exe.

gouzao.exe - 24 error(s), 3 warning(s)
加了后还是出现一大队错误。
真不知该怎么改.
望指点

#4
yuyunliuhen2007-01-03 13:09

没这么多吧

[此贴子已经被作者于2007-1-3 13:12:35编辑过]

#5
chenjiang0012007-01-03 13:16
我也不知道
#6
peswe2007-01-03 14:50
呵呵,也是新手吧,都会犯同样的错误,我也经常犯呢:
你的类名后面多了个括号,你把他当函数来使用了!~
去掉后就OK了!~
#7
pusawl2007-01-03 16:52
哈哈,有人没看出来~~
#8
kai2007-01-03 17:07
[CODE]#include <iostream>
using namespace std;

class Location
{
public:
Location(int xx=0,int yy=0);
Location(Location & p);
~Location();
int GetX(){return X;}
int GetY(){return Y;}
private:
int X,Y;
};
Location::Location(int xx,int yy)
{
X=xx;
Y=yy;
cout<<"constructor Object.\n";
}
Location::~Location()
{
cout<<X<<","<<Y<<"objiect destroyed."<<endl;
}
Location::Location(Location & p)
{
X=p.X;
Y=p.Y;
cout<<"copy constructor called.\n";
}
void f(Location p)
{
cout<<"Function"<<p.GetX()<<","<<p.GetY()<<endl;
}

int main()
{
Location B(1,2);
f(B);

return 0;
}

[/CODE]
#9
墨岛2007-01-03 18:40
class Location()
哪有这个括号,去掉。
#include "stdio.h"
这个也多余,去掉
ok
1