Undeclared Identifier, but it is declared!!

I have a program with a class in different files, I've included the header file but it still keeps saying 'undeclared identifier'


class header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef SEARCHING_H
#define SEARCHING_H

#include "Assignments.cpp"

class searching {
	public:
		searching();
		int menu();
		int searchClass(task tasks[], int num, int searchedTasks[]);   //Searches by Class
		int searchAssign(task tasks[], int num, int searchedTasks[]);  //Searches by Assignment
		void displaySearch(task tasks[], int searchedTasks, int num);
	private:
		int choice;
		int count;
		char searchName[100];
};


#endif 


Main File:
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

#include "searching.h"

...........
...........
...........


//Processes the users command and decides what to do with it
void processCommand(const int com, task tasks[], int& num) {
	int taskNum;
	int searchCom;
	int searchedTasks[100] = {0};
	int searchCount;
	switch(com) {
	case 1:
		listTasks(tasks, num);
		break;
	case 2:
		inputTask(tasks, num);
		break;
	case 3:
		if(num > 0) {
			cout << "What is the number of the task you want to delete?: ";
			cin >> taskNum;
			if((taskNum <= num) && (taskNum > 0)) {
				deleteTask(tasks, taskNum, num);
			}else {
				cout << "Number " << taskNum << " not valid.\n";
			}
		}else {
			cout << "There are no tasks to delete!!\n";
		}
		break;
	case 4:
		searching search;
		searchCom = search.menu();
		switch(searchCom) {
			case 1:
				searchCount = search.searchClass(tasks, num, searchedTasks);
				break;
			case 2:
				searchCount = search.searchAssign(tasks, num, searchedTasks);
				break;
		}
		if(searchCount == 0) {
			cout << "No results to display!\n";
			pause();
		}else {
			search.displaySearch(tasks, searchedTasks, searchCount);
			pause();
		}

	case 5:
		cout << "Bye Bye!\n";
		break;
	}
}


The problem happens at case 4 when I try to declare the object searching search;

I've tried to find the answer elsewhere but nothing I found worked.
Declaring new variables within a case is not possible without enclosing the case within brackets.

Try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case 4:
{
		searching search;
		searchCom = search.menu();
		switch(searchCom) {
			case 1:
				searchCount = search.searchClass(tasks, num, searchedTasks);
				break;
			case 2:
				searchCount = search.searchAssign(tasks, num, searchedTasks);
				break;
		}
		if(searchCount == 0) {
			cout << "No results to display!\n";
			pause();
		}else {
			search.displaySearch(tasks, searchedTasks, searchCount);
			pause();
		}
}
Ok.... I tried that but it still says 'Undeclared Identifier!' I even tried putting the object declaration outside of the switch statement, but the error is still there.
That's goofy. I do believe that the brackets are necessary when declaring variables within a case so you'll need that eventually. I'm always skeptical when someone does a partial copy and paste as you have done. It makes me wonder if I am really looking at exactly what you are trying to compile. Is there a typo in the file name for the header class? Is the compiler really finding the header file at all? I would start looking for silly mistakes like that.

By the way, what OS are you using? Linux or Windows? Does the following thread help you at all?
http://www.cplusplus.com/forum/unices/13364/
1
2
3
4
5
6
7
8
9
#ifndef SEARCHING_H
#define SEARCHING_H

// Don't do this!
#include "Assignments.cpp"

class searching {
	public:
...


Don't include cpp files. Add them to your project, compile and link them.
Good catch. I didn't even notice that.
Hi guys, I've been having the same problem for a while now. I'm pretty sure the problem is with visual c++ because I've made several alterations to my code with no luck, and I tried writing a very simple program to see whether I could declare an object and it still giving me the 'undeclared identifier' error.

I also copied the code to a linux computer using codeblocks and the same code ran fine.

Any help would be appreciated

Header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef DATE_H
#define DATE_H
 
class Date
{
private:
    int m_nMonth;
	int m_nDay;
    int m_nYear;
 
    Date() { } 
 
public:
    Date(int nMonth, int nDay, int nYear);
 
    void SetDate(int nMonth, int nDay, int nYear);
 
    int GetMonth() { return m_nMonth; }
    int GetDay()  { return m_nDay; }
    int GetYear() { return m_nYear; }
};
 
#endif 


Source file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Date.h"

using namespace std;

 

Date::Date(int nMonth, int nDay, int nYear)
{
    SetDate(nMonth, nDay, nYear);
}
 

void Date::SetDate(int nMonth, int nDay, int nYear)
{
    m_nMonth = nMonth;
    m_nDay = nDay;
    m_nYear = nYear;
}


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Date.h"

using namespace std;



int main()
{
	Date date; //Everything is fine untill this point
		//this is when I get the undeclared identifier error
	
	return 0;
}


Thanks
> Date date;

You would get an error for this with any C++ compiler - the default constructor of Date is private.

I changed it to public and I still got the same error
Are you using a pre-compiled header file?
If yes, turn off the use of pre-compiled headers; clean; and recompile.
Pre-compiled header file was already turned off. Ive even re-installed VC++ and still no luck.
> Pre-compiled header file was already turned off.

Ok. Now try changing the include guard.

1
2
3
4
5
//#ifndef DATE_H
//#define DATE_H

#ifndef DATE_H_HAS_BEEN_INCLUDED
#define DATE_H_HAS_BEEN_INCLUDED 
Topic archived. No new replies allowed.