I 've linked the two files together, main.cpp and myClasses.h, I've declared a variable, but what I'm asking is can you access a variable from myClasses.h?

Here's a example of what I got so far:

//main.cpp
#include "myClasses.h"
#include <iostream>

using namespace std;

int main()
{
   ClassOne myClass_instance;
   s = 5;
   cout << "The number is: " << s;

  //etc....
}

//myClasses.h
// MyClasses.h
#ifndef MYCLASSES_H   // note: these are very important!
#define MYCLASSES_H

class ClassOne
{
    public:
      int s;
      
};

#endif

The error i'm getting is something like : s undeclared.
Thanks for the help.

Member Avatar for iamthwee

Some info would be usefu such as:

-What OS are you using?
-What compiler or IDE are you using?
-Are those two files saved in the same project, or directory etc?
-How are you compiling it, through the command line or by hitting the compile run button?

-If it isn't an issue to do with linking maybe your class defo or main declaration is wrong.
-Have you tried working with the whole thing in one file?

What iamthwee said is all true, but your problem is here: (in red)

ClassOne myClass_instance;
s = 5;
cout << "The number is: " << s;

I think you mean something like:

ClassOne myClass_instance;
myClass_instance.s = 5;
cout << "The number is: " << myClass_instance.s;
commented: Great! Thanks for helping me out! +1

you can use like Object . Variable name;

you can use like Object . Variable name;

How is that different from what I said?

Thanx for the Help. Your suggestion solved the errors!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.