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:

#include "myClasses.h"
#include <iostream>

using namespace std;

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


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

class ClassOne
      int s;


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

8 Years
Discussion Span
Last Post by Dannyo329

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;
Votes + Comments
Great! Thanks for helping me out!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.