Undeclared indentifiers

Could of sworn that i decalred x and n however c++ seems to think otherwise...

code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>

using namespace std;

template <class T>
class Number
{
public:
	T x;

	Number(T x, T n)
	{
		x = 0;
		n = 0;
	}

	void Set(T n)
	{
		n = 9;
	}

	int Get()
	{
		return T n;
	}

private:
	T n;

public:

bool isEqual(T x)
{
	Set(n);

	if (x==n)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool isBigger(T x)
{
	Set(n);

	if (x>n)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool isSmaller(T x)
{
	Set(n);

	if(x<n)
	{
		return true;
	}

	else
	{
		return false;
	}
}

};

int main()
{
	cout << "Worksheet 12 task 3" << endl;

	Number <int> num(x); // undeclared identifier

	cout << num.isEqual(x) << endl; // undeclared identifier
	cout << num.isBigger(x) << endl; // undeclared identifier
	cout << num.isSmaller(x) << endl; // undeclared identifier

	cin.ignore(2);
}
in your main function what is x you didn't declarer any variable x your passing a variable which is not defined in your main function
Ok and how would i pass a variable to my main function
main is declared as int main(int argc, char *argv[]). It takes all arguments given to your program at command line as char arrays, one for each argument. argv[0] is set to the command/program name. The argv array is terminates with the value NULL. argc counts the number of arguments including argv[0] but without the NULL.

Example: May be your program will be called mysupertruperapp then the call
mysupertruperapp Hello World results in
argv[0] = "mysupertruperapp"
argv[1] = "Hello"
argv[2] = "World"
argc = 3
Please post the exact errors you are receiving. Also, please state your IDE. I have a hunch that passing arguments into main() is not what you want to do.

My glance-over intuition tells me :

You need a variable named 'x' in main.
You need a Number::Number(T i) constructor, or modify your old constructor to give 'n' a default value.
As xismn said, you're using a variable called x in main, but you haven't declared it, nor have you given it an initial value.

I notice your Number template class contains a public data member x. Is that really what you intended? None of the methods of Number use it, and many of them hide that data member by declaring an argument of the same name.
Topic archived. No new replies allowed.