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 8372447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:26:04+00:00 2026-06-09T14:26:04+00:00

I’m learning about classes and objects in c++ and tried the following code to

  • 0

I’m learning about classes and objects in c++ and tried the following code to test if I understood it properly:

#include <iostream>

using namespace std;

class class1
{
    public:
        void write(int x)
        {
             dataObject.var = x;
        }
};

class class2
{
    public:
        void read()
        {
            std::cout << dataObject.var;
        }
};

class data
{
    public:
        int var;
        data()
        {
            var = 1;
        }
};

int main()
{
    data dataObject;
    class1 object1;
    class2 object2;


    object2.read(data dataObject);
    object1.write(2);
    object2.read(data dataObject);

    return 0;
}

This was for two objects to both be used to modify and use the members of a third but I get the following errors:

In member function 'void class1::write(int)':
line 10: error: 'dataObject' was not declared in this scope

In member function 'void class2::read()':
line 14: error: 'dataObject' was not declared in this scope

In function 'int main()':
line 40 + 42: error: expected primary-expression before 'dataObject'

Any idea where I am going wrong?
Thanks in advance.

EDIT:
Thanks for all the suggestions. My code now is:

#include <iostream>

using namespace std;

class class1
{
    public:
        void write(data &dataObject, int x)
        {
            dataObject.var = x;
        }

};

class class2
{
    public:
        void read(data dataObject)
        {
            std::cout << dataObject.var << endl;
        }
};

class data
{
    public:
        int var;
        data()
        {
            var = 1;
        }
};

int main()
{
    data dataObject;
    class1 object1;
    class2 object2;


    object2.read(dataObject);
    object1.write(dataObject,2);
    object2.read(dataObject);

    return 0;
}    

And I now get the errors:

8 error: ‘data’ has not been declared

10 error: request for member ‘var’ in ‘dataObject’, which is of non-class type ‘int’

18 error: ‘data’ has not been declared

20 error: request for member ‘var’ in ‘dataObject’, which is of non-class type ‘int’

40 error: ‘dataObject’ was not declared in this scope

  • 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. Editorial Team
    Editorial Team
    2026-06-09T14:26:05+00:00Added an answer on June 9, 2026 at 2:26 pm

    Where you go wrong is that at this point:

        void write(int x)
        {
             dataObject.var = x;
        }
    

    there is no dataObject. You are trying to make your classes depend on some global object. First of all you have to decide if you really want this. And if you do, you need a way to ensure that these global objects are declared and instantiated before they get used by the classes.

    There are many ways to fix the error, but first you need to be clear about what you want the classes to do and how they should interact with each other.

    One example of how you could fix this without global objects:

    class class1
    {
      public:
        class1(data& dataObj) : dataRef_(dataObj) {}
        void write(int x)
        {
          dataRef_.var = x;
        }
      private:
        data& dataRef_;
    };
    

    Then in the main:

    int main()
    {
      data dataObject;
      class1 object1(dataObject);
      object1.write(2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.