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

1>e:\程序\c++\tbar_foo\tbar_foo\Bar_Foo.h(20) : error C2059: 语法错误 : “常量”

陈子风 发布于 2008-05-17 19:44, 2932 次点击
#include<iostream>
using namespace std;
class Foo
{
public:
    Foo(int f)
    {  fooval=f;  }
    int GetVal()
    {
        return fooval;
    }
private:
    int fooval;
    friend class Bar;
};
class Bar
{
private:
    //static  int barval;
    static Foo foo(5);
    int callsFooVal;
public:
    Bar()
    {
        callsFooVal=0;
    }
    
};


1>e:\程序\c++\tbar_foo\tbar_foo\Bar_Foo.h(20) : error C2059: 语法错误 : “常量”

不知道错哪里了    哪位高手指点一下
6 回复
#2
陈子风2008-05-17 19:54
static Foo foo(5);处
#3
julian12092008-05-17 20:10
e:\程序\c++\tbar_foo\tbar_foo\Bar_Foo.h(20) : error C2059: 语法错误 : “常量”
靜態成員只能是靜態數據成員和靜態成員函數(靜態成員函數只能訪問內部靜態成員),類不能作為靜態成員
#4
陈子风2008-05-18 18:00
不太明白,能否解释清楚点儿。改成这样为什么就没有编译错误呢:

#include<iostream>
using namespace std;
class Foo
{
public:
    //Foo(int f)      
   // {  fooval=f;  }   去掉构造方法
    int GetVal()
    {
        return fooval;
    }
private:
    int fooval;
    friend class Bar;
};
class Bar
{
private:
    //static  int barval;
    static Foo foo;       //static Foo foo(5);
    int callsFooVal;
public:
    Bar()
    {
        callsFooVal=0;
    }
   
};
#5
flyue2008-05-18 19:55
static Foo foo(5);是什么语法啊?我也看不懂,貌似不是C++的语法
#6
sunkaidong2008-05-18 20:20
不一定要是静态的啊..静态的要在外面赋值...
#7
陈子风2008-05-18 22:29
这是C++ primer 书中的一道题目,“给上题中定义的Foo类定义另一个类Bar类。

bar类具有两个static数据成员:一个为int型,另一个为Foo类型。

顺便问一下,谁有《C++ Primer》书后面习题的答案的啊。可以发给我吗?

wangpengjl@

谢谢!
1