Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.Please,help me.I want to ask that Why does " ‘x’ and 'y' was not declared in this scope" this error seem(on place of show function)? I think, they were declared like parameters of the function.Weren't they?

C++
#include<iostream>
using namespace std;
template<class T>
class myMax{
 	T a,b;
public:
      void set(T x,T y);
	T show()
{
	return(x>y)?x:y;
}

};

template<class T>
void myMax<T>::set(T x,T y)
{
	a=x;
	b=y;
}


//_____________________________________________________

int main()
{
	myMax<int>a;
	myMax<int>a1;
	myMax<double>b;
	a1.set(5,19);
	a.set(0,-78);
	b.set(90,89.9);	
    cout<<a1.show()<<endl
	<<a.show()<<endl
	<<b.show()<<endl;	
	return 0;
}


What I have tried:

I also tried other variable names,but only a and b worked.
Posted
Updated 27-Aug-16 10:14am
v2

Quote:
I think, they were declared like parameters of the function.
Where do you see them declared in this function ?
C++
	T show()
{
	return(x>y)?x:y;
}

May be you should use a and b instead ?
[Update]
Quote:
they are declared for set function.
and why can't i use other variables?
They declared in set function for set function only.
[Update]
In show, you can use a and b because you declared them in class definition.
C++
class myMax{
 	T a,b;
 
Share this answer
 
v3
Comments
Dave Kreskowiak 27-Aug-16 15:27pm    
The concept is called "scope". Maybe you should read up on it.

Variables are only usable inside the scope they are defined. Since you defined them in the scope of the set method they are not visible anywhere outside of that method (scope).

My C++ is quite rusty but it would appear that you need to explicitly set the values of the fields a and b using the values passed in the set method as x and y. You can then return the maximum value of a and b from your show method instead of using the x and y values.
Member 12702056 27-Aug-16 15:38pm    
but why i can't use (not x,y)/for example k,f instead of a and b?i understood that x,y are parametres of another function,so they are used only in their function scope.
but i mean, why i can't use other variable names instead of a and b?
i think,there must be reason for this,but i can't find that reason.
do you know it?If you know,please let me know
Member 12702056 27-Aug-16 15:59pm    
Thanks much :)
Member 12702056 27-Aug-16 15:38pm    
ppolymorphe ,they are declared for set function.
and why can't i use other variables?
is this related with that we set x to a and y to b,and then must we use for showing only those variables?
Patrice T 27-Aug-16 15:41pm    
I just updated my solution.
The code that you are showing,
C++
T show()
{
	return(x>y)?x:y;
}

Is invalid, by looking at the class definition (have a look at the members!). You have defined only a and b to be properties in the class, x and y are not given to be of any type, and thus no defined in the given scope.
Quote:
only a and b worked.
That is because a and b are defined as properties (members) in your class of type T. That executes perfectly. Because compiler knows what type to use and where they are defined in the scope (as the members).

Solution to this problem is to just use the a and b names. Don't use the x and y; because that would require to change the class definition too!
 
Share this answer
 
Comments
Member 12702056 27-Aug-16 16:02pm    
Afzaal Ahmad Zeeshan,thank you very much
Finally,i got what i wanted to understand exactly :)
 
Share this answer
 
v2
Comments
Member 12702056 27-Aug-16 16:21pm    
ppolymorphe,ok,thanks :)
I'll try .
Patrice T 27-Aug-16 16:24pm    
Save the books on your PC, and have a good lecture :)
Member 12702056 27-Aug-16 16:40pm    
ok :)
but i am beginner in c++ ,i began this year,so beacuse of this i learn from videos about c++ more than books.but i will try books also,thanks :)
Patrice T 27-Aug-16 16:42pm    
The books are references
C book is by the authors of C language.
C++ book is by the author of C++ language.
Member 12702056 27-Aug-16 16:45pm    
I got what you mean :) that it should be better than videos :)
I saw that you are professional programmer from your profile . Can i ask you that i want to be c++ debugger,what must i do for it? Also for knowing this language better ,what must i do? :) can you give me advice,please?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900