Hi.
I am trying to create a class for my program and use it in different functions. I declare in one function a name for class and then I try to call this names from different functions the program doesn’t allow me. Its a bit hard to explain so i will post a sample code so you could help me out.
#include <iostream>
using namespace std;
class Dog
{
public:
string name;
};
void funExample1()
{
Dog Name1, Name2; //I make Names for class so i can have 2 dogs
cin>>Name1.name; //Writing in name for first dog
cin>>Name2.name; //writing in name for second dog
}
int main()
{
funExample1(); //the beginning. I call for function funExample1
cout<<"Fist dogs name is: "<<Name1.name<<"Second dogs name is: "<<Name2.name<<endl;
return 0;
}
The program says:
error: 'Name1' was not declared in the scope
error: 'Name2' was not declared in the scope
Code tags.
You can also replace class with struct, structs are public by default.
And it's because name1 and name2 don't exist in main. Look at them. They go out of scope when funexample ends and are destroyed.
void funExample1()
{
Dog Name1, Name2; //I make Names for class so i can have 2 dogs
//here you make the 2 dogs
cin>>Name1.name; //Writing in name for first dog
cin>>Name2.name; //writing in name for second dog
//this is the end of the function funExample1's scope, so the local variables (Name1, Name2) are destroyed
}
Because the objects name1 and name2 are defined within the function, they are limited to function scope. When that function's execution ends the variables are destroyed. THEY NO LONGER EXIST. That means you cannot access them in main.
You could declare them globally or pass them into the function instead. Or you could just put that code in main since the function doesn't really have a purpose.
You need to create objects for Class Dog, which are name1 and name 2 in the main. YOu are creating objects inside a function which is not correct. Try creating objects inside the main..
kavithab, If i create objects inside main and I use them in main I have no problem, but if I create object in main and for example I would like to use it in some function I will get same error message. As well I need to use it this way but its not possible or maybe I dont know how to do it. So I am asking maybe someone knows something about my little problem. Thanks.
A class is not special when it comes to allocation. If you declare it in scope, it dies after that scope ends. That's the way it works for all variables.
Do the same thing you would for a normal variable, classes are no different. At this essential level, a class is a plain old datatype (that's a term, POD) - all it is is a package of other types. It's a data type and nothing else. So treat it the same way.
using namespace std;
class Dog
{
private:
char first[10],second[10];
public:
void funExample1();
void print();
};
void Dog::funExample1() //:: use this scope resolution operator like this-- function_return_type class_name::function_name() {...}
{
cout<<"enter First dog's name"<<endl;
cin>>first;
cout<<"enter 2nd dog's name"<<endl;
cin>>second;
}
void Dog::print() { //:: use this scope resolution operator like this-- function_return_type class_name::function_name() {...}
cout<<"dogs names are "<<first<<"\t"<<second<<endl;
}
int main()
{
Dog Name1, Name2; //creating 2 objects for class Dog
Name1.funExample1(); //the beginning. I call for function funExample1
Name1.print(); //printing the names of the dogs
return 0; //indicates that the program compiled successfully
}
Run this example on your gcc and see if this works.. Works for me. This should explain to you how this code works. You are almost there :) Hope this helps!
Outside all functions? In the scope of your includes?
Review the tutorial in the documentation. It's gotta be in there somewhere and I'm not giving you examples for something this simple. If you're already on classes but you haven't got this I can't help you yet.
kavithab, Hey thanks for time, but there is one problem. I dont want to change my functions because it would mess up my real code :( and i would spend ages to write it all down just for adding two names :D i could just declare string name1,name2; ,but because I am in uni and I am writing program for assignment I need to use class somethere and to write in names using class would be just perfect for my program.