Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 131401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:05:58+00:00 2026-05-11T06:05:58+00:00

The following for creating a Global Object is resulting in compilation errors. #include stdafx.h

  • 0

The following for creating a Global Object is resulting in compilation errors.

#include 'stdafx.h'  #include <iostream>   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 << 'Welcome!' << endl;          while(1)         {             input();         } }  void input() {     int newNumber = 0;      cout << 'The current number is ' << testerObject.getNumber();     cout << 'Change number to: ';          cin >> newNumber;      cout << endl;      testerObject.setNumber(newNumber);      cout << 'The number has been changed to ' << testerObject.getNumber() << endl; } 

Here are the compile errors:

1>------ Build started: Project: test, Configuration: Debug Win32 ------ 1>Compiling... 1>test.cpp 1>.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class 1>.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject' 1>.\test.cpp(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>.\test.cpp(49) : error C2039: 'getNumber' : is not a member of 'System::Int32' 1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32' 1>.\test.cpp(55) : error C2039: 'setNumber' : is not a member of 'System::Int32' 1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32' 1>.\test.cpp(57) : error C2039: 'getNumber' : is not a member of 'System::Int32' 1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32' 1>Build log was saved at 'file://c:\Users\Owner\Documents\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm' 1>test - 6 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
  1. How do I create a Global Class Object correctly like I’ve attempted here.
  2. And how do I fix that ‘only static const integral data members can be initialized within a class’
  3. And basically how do I fix the rest of the errors so I can get this to compile?

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!

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 & 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:05:59+00:00Added an answer on May 11, 2026 at 6:05 am

    Just start addressing the errors one by one. A lot of the errors are just cascaded from the initial errors, so it looks like there are a lot of problems when there’s only a couple. Just start from the top:

    1>.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class 

    You can’t initialize a member in the class definition unless it’s static, const, and one of the integral types. Leave the ‘= 5‘ off of the declaration of number. Then you’ll need to have a definition of Tester::number outside of the class definition, like so:

    int Tester::number = 5; 

    Problem #2:

    1>.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject' 

    Almost exactly what it says (missing semi-colon errors can be a bit inexact in saying where the semicolon should be) – you need a semi-colon after the definition of the Tester class.

    Fix those and your compilation problems go away.

    The key thing is to try and take compiler errors one at a time from the top. If you get more than about 3 of them, you can probably just ignore everything after the 3rd or so because the initial error just cause the compile to into the weeds (and if they are real errors, they’ll show up again in the next compile anyway).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A quick google led me to RS232 Data Logger -… May 12, 2026 at 12:44 am
  • Editorial Team
    Editorial Team added an answer No, ID is an attribute of the tag. So if… May 12, 2026 at 12:44 am
  • Editorial Team
    Editorial Team added an answer The login window application is defined as part of the… May 12, 2026 at 12:44 am

Related Questions

Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
I'm creating an WPF app, so I'm mostly working with the ImageSource class for
I am currently in the process of rewriting an application whereby teachers can plan
We have a very large JavaScript application, where after many months of coding there

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.