0

I have two files sampleclass.cpp and sampledataclass.cpp along with sampleclass.h and sampledataclass.h

In sampleclass.cpp in the constructor I give a value to a variable

sampleclass.cpp
sampleclass:sampleclass(){
x = "some string value";
}

In sampleclass.h I create x and an instance of the data class
sampleclass.h
CString x;
sampledataclass myDC;

Now in the constructor of the data class I want to be able to set the value of another string to the value of x
sampledataclass.cpp
sampledataclass:sampledataclass(){
CString y = mysampleclass->x;
}

The h file has this
sampleclass.h
sampleclass *mysampleclass;

How do I do this without getting a runtime error

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by ArkM
0

Never define global variables in .h files! You can declare these variables, of course - simply use extern keyword before its type, but define such variables in one and only only cpp file.
As usually, there are better solutions than global variables using, but it's the other story.

The key question in your case: who (and when) must initialize (global?) mysampleclass pointer variable? I don't know the answer because your snippet is too laconic.

It seems you have too cumbersom mechanics for a simple task. May be you present more codes and better your program case description?

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.