既出の可能性が高い質問ですがごめんなさい。。。
C++で書いたクラスのstatic変数の宣言の仕方に戸惑い、面倒臭い状態になっています。
症状としては、コンパイルするたびに変数の宣言を消す、書く、消す、書く(LNK2001未定義とLNK2005定義されています)
という状況になっています。。
プログラム
どう改善すれば良いのか教えて頂きたいです><
クラス内のstatic変数のエラーについて(LNK2001&LNK2005)
Re: クラス内のstatic変数のエラーについて(LNK2001&LNK2005)
こんにちは
In a.cpp don't use
<code>
int A::size;
</code>
You already declared "size" by including a.h
In a.cpp don't use
<code>
int A::size;
</code>
You already declared "size" by including a.h
Re: クラス内のstatic変数のエラーについて(LNK2001&LNK2005)
ありがとうございます
解決できました!
We were able to solve
Thanks you! :)
追記:問題の起きたプログラムで、ヘッダではなくcppをインクルードしていました…
解決できました!
We were able to solve
Thanks you! :)
追記:問題の起きたプログラムで、ヘッダではなくcppをインクルードしていました…
Re: クラス内のstatic変数のエラーについて(LNK2001&LNK2005)
I don't think that this is right.faserg1 さんが書きました:こんにちは
In a.cpp don't use
<code>
int A::size;
</code>
You already declared "size" by including a.h
"size" is declared but not defined in a.h
Therefore, is needed in a.cpp or somewhere else.
c++ - What is the difference between a definition and a declaration? - Stack Overflow
sizeはa.hで宣言されているだけで定義されていないので、どこかに定義を書かないといけません。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)
Re: クラス内のstatic変数のエラーについて(LNK2001&LNK2005)
いいえ、that's wrong think.みけCAT さんが書きました:I don't think that this is right.
"size" is declared but not defined in a.h
Also, problem already solved.
There we already declared AND defined A::size.
The problem, as I understand, was next – in main.cpp was included a.cpp
So, linker will say "A::size defined many times"