Note that there are some explanatory texts on larger screens.

plurals
  1. POResolving "only static const integral data members can be initialized within a class" compilation error
    primarykey
    data
    text
    <p>The following for creating a Global Object is resulting in compilation errors.</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; using namespace System; using namespace std; #pragma hdrstop class Tester; void input(); class Tester { static int number = 5; public: Tester(){}; ~Tester(){}; void setNumber(int newNumber) { number = newNumber; } int getNumber() { return number; } } Tester testerObject; void main(void) { cout &lt;&lt; "Welcome!" &lt;&lt; endl; while(1) { input(); } } void input() { int newNumber = 0; cout &lt;&lt; "The current number is " &lt;&lt; testerObject.getNumber(); cout &lt;&lt; "Change number to: "; cin &gt;&gt; newNumber; cout &lt;&lt; endl; testerObject.setNumber(newNumber); cout &lt;&lt; "The number has been changed to " &lt;&lt; testerObject.getNumber() &lt;&lt; endl; } </code></pre> <p>Here are the compile errors:</p> <pre><code>1&gt;------ Build started: Project: test, Configuration: Debug Win32 ------ 1&gt;Compiling... 1&gt;test.cpp 1&gt;.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class 1&gt;.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject' 1&gt;.\test.cpp(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1&gt;.\test.cpp(49) : error C2039: 'getNumber' : is not a member of 'System::Int32' 1&gt; c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32' 1&gt;.\test.cpp(55) : error C2039: 'setNumber' : is not a member of 'System::Int32' 1&gt; c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32' 1&gt;.\test.cpp(57) : error C2039: 'getNumber' : is not a member of 'System::Int32' 1&gt; c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32' 1&gt;Build log was saved at "file://c:\Users\Owner\Documents\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm" 1&gt;test - 6 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== </code></pre> <ol> <li>How do I create a Global Class Object correctly like I've attempted here.</li> <li>And how do I fix that "only static const integral data members can be initialized within a class"</li> <li>And basically how do I fix the rest of the errors so I can get this to compile?</li> </ol> <p>I like declaring Global Class Objects at file scope (I like declaring all globals at file scope) because when I have to create separate source files and do "extern" and everything it becomes extremely complicated and never works for me. Although, I do want to figure out how to do that eventually... it seems every tutorial I look at won't compile though and unless it compiles I have no idea how to recreate it!</p> <p>If I can just get this to compile...then I can successfully learn how to do this. So if someone could rewrite the above to where it literally copies &amp; pastes into Visual C++ Express 2008 and works I will finally be able to figure out how to recreate it. I'm extremely excited on seeing the fix for this! It is just I can't get Global Objects to work right! Any other information on declaring Global Class Objects...or anything for that matter is welcome!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload