一段极简单的程序VC6不能通过编译,为什么(20分)?

goblinize 2002-08-31 06:56:18
//--------------test.h----------------
#include <vector>
using namespace std;
class test
{
public:
test(int capacity):_tast(capacity){}//这么写对马?
int capacity;
}

//------------test.cpp----------------
#include <iostream>
#include "test.h"
using namespace std;

void main()
{
int cap;
cin>>cap;
test t(cap);
cout<<t.capacity;
system("pause");
}
得到如下报错:
Compiling...
test.cpp
c:\windows\desktop\test\test.cpp(3) : error C2143: syntax error : missing ';' before 'PCH creation point'
Error executing cl.exe.

test.exe - 1 error(s), 0 warning(s)
别告诉我用DEV,我机器装不了!
难道真的又是因为命名空间的问题?
...全文
37 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
alexxing 2002-09-02
  • 打赏
  • 举报
回复
“test构造函数的形参和定义test对象时的实参参数名可以不一样呀”?
老兄的眼神真是可以,再仔细看看?

问题不是形参与实参是否同名,而是形参 capacity 与成员变量 capacity 重名了!

class test
{
public:
test(int cap):_tast(cap), capacity(cap) {}
// capacity(cap) 是给 capacity 赋初值!
// 把 cap 换成 capacity,成了什么?
vector <int> _tast;
int capacity; //这是什么?
};

int cap;
cin>>cap;
test t(cap);
cout<<t.capacity; //这个 t.capacity; 哪里来的?赋值了吗?
system("pause");

你的 t.capacity 根本没赋值,所以输出 -858993460 (随机数)
下次再运行,估计又是另一个随机数
goblinize 2002-09-02
  • 打赏
  • 举报
回复
没见过!!我刚学C++
别看我三个三脚,以为我是牛人,都是在98区得的。
cBeginner 2002-09-02
  • 打赏
  • 举报
回复
//--------------test.h----------------
#include <vector>
using namespace std;
class test
{
public:
test(int capacity):_tast(capacity){}//这么写对马?
int capacity;
}; // here

//------------test.cpp----------------
#include <iostream>
#include "test.h"
using namespace std;

void main()
{
int cap;
cin>>cap;
test t(cap);
cout<<t.capacity;
system("pause");
}
elvahuang 2002-09-01
  • 打赏
  • 举报
回复
率哥你要写就写完嘛
我到处找_tast
Lostinet 2002-09-01
  • 打赏
  • 举报
回复
倒。。
是不是把
#include "stdafx.h"
扔了??

把:
工程=》属性=》C++=》分类[Precompiled Headers]=》(第一个N)不使用预补偿页眉
选上了。。
blh 2002-09-01
  • 打赏
  • 举报
回复
class A{
};//必须有;
alexxing 2002-09-01
  • 打赏
  • 举报
回复
class test
{
public:
test(int cap):_tast(cap), capacity(cap) {}
vector <int> _tast;
int capacity;
};
goblinize 2002-09-01
  • 打赏
  • 举报
回复
类定义的分号加上了,头文件改为:
#include <vector>
using namespace std;
class test
{
public:
test(int capacity):_tast(capacity){}
vector <int> _tast;
int capacity;
};
可以通过编译,但是结果不对:
我输入了一个3,但输出-858993460。为什么会这样?
MasterProgrammer 2002-09-01
  • 打赏
  • 举报
回复
晕...
Lostinet 2002-09-01
  • 打赏
  • 举报
回复
倒???

#include "stdafx.h"
没有见过??
VC6的每个工程的CPP都有这个的哦。。
goblinize 2002-09-01
  • 打赏
  • 举报
回复
Lostinet(迷失网络) :
工程=》属性=》C++=》分类[Precompiled Headers]=》(第一个N)不使用预补偿页眉
在什么地方????我用的是VC6,"工程"里面没有"属性"!
#include "stdafx.h"干鸡毛用的?没见过!!

alexxing(赤铸) :
你写的我看不懂!test构造函数的形参和定义test对象时的实参参数名可以不一样呀!
test(int capacity):_tast(capacity){}
有什么不对吗???
goblinize 2002-09-01
  • 打赏
  • 举报
回复
不好意思,匆忙中忘掉了!!
xiaojunsyiae 2002-08-31
  • 打赏
  • 举报
回复
不知道你是否想显示你的初始化capacity的值,如果是的话,你看看:#include <vector>
#include <iostream>

using namespace std;

class test
{
public:
test(int capacity); //这么写对马?
int capacity;
};

test::test(int n)
{
test::capacity=n;
}

void main()
{
int cap;
cin>>cap;
test t(cap);
cout<<t.capacity;
system("pause");
}
tigerfox 2002-08-31
  • 打赏
  • 举报
回复
按F4看看到了哪 ,上下看看什么地方错了。
lw_yx 2002-08-31
  • 打赏
  • 举报
回复
加上分号后再看看哪错
lw_yx 2002-08-31
  • 打赏
  • 举报
回复
类的定义完了少“;”自已好好看看。这是最常见的错误
skull 2002-08-31
  • 打赏
  • 举报
回复
what is "test(int capacity):_tast(capacity){}" ,
would your want "test(int cap):capacity(cap) {}"?

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧