陈子风 发表于 2008-5-17 19:44

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

#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: 语法错误 : “常量”

不知道错哪里了    哪位高手指点一下

陈子风 发表于 2008-5-17 19:54

static Foo foo(5);处

julian1209 发表于 2008-5-17 20:10

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

靜態成員只能是靜態數據成員和靜態成員函數(靜態成員函數只能訪問內部靜態成員),類不能作為靜態成員

陈子风 发表于 2008-5-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;
    }
   
};

flyue 发表于 2008-5-18 19:55

static Foo foo(5);是什么语法啊?我也看不懂,貌似不是C++的语法

sunkaidong 发表于 2008-5-18 20:20

不一定要是静态的啊..静态的要在外面赋值...

陈子风 发表于 2008-5-18 22:29

这是C++ primer 书中的一道题目,“给上题中定义的Foo类定义另一个类Bar类。

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

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

wangpengjl@sina.com

谢谢!

页: [1]

编程论坛