#include<stdio.h>
#include<vector>
#include<iostream>
using namespace std;
int x = 1;
class foo
{
public:
foo()
{
x = 3;
}
static int x;
void bar() const
{
cout << x << endl;
}
int x;
};
int foo::x = 2;
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
は、ここでは、コンパイラの出力です:静的データメンバーが非静的データメンバーと同じ名前を持つことができないのはなぜですか?
test.cc:19:9: error: ‘int foo::x’ conflicts with a previous declaration
int x;
^
test.cc:14:16: note: previous declaration ‘int foo::x’
static int x;
同じスコープ内で関数内で同じ名前の変数を2つ持つことはできません(同じものが静的であれば問題ありません)。 – eshb
そうでなければ、どちらがどちらだったか分からないからです。このプログラムはあいまいです。 – EJP