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.

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;
Comments
Great! Thanks for helping me out!
This article has been dead for over six months. Start a new discussion instead.