Completed program: keep getting error message "...was not declared in this scope"

Hey guys. So I wrote this program. What I've done here is made a class and struct in one file that feed into my .cpp file of the functions. I can explain more about what my program does if really necessary, but I don't think it really is here, because I'm done with it. I just keep getting this error message that I just don't get.

Ok, here's my class .h 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
#include <iostream>
#include <cstring>
#include <cctype>
#include <fstream>


using namespace std;
const int SIZE = 15;
struct houses
{
        int rent;
        int size;
        int rooms;
        char*location;
        char*view;
        char*smoking;
};

class Housing
{
        public:
                void load(char filename[]);
                Housing();
                ~Housing();
                void prioritize();
                void Display_all();
                void Display_top(int x);
        private:
                houses houses1[SIZE];
                char temp[100];
                char filename[];
                int selection;
};

---
and here's the part of my .cpp file that keeps getting errors in the compiler (I can provide the whole file if necessary, but for now I'll just show you my void prioritize function):

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "hmwk4.Documents.h"
using namespace std;

int main()
{
        Housing*stuff;
        stuff = new Housing;
        stuff -> load("houses1.txt");
        stuff -> prioritize();

        cout<<"\nThis is all your shit: \n";
        stuff -> Display_all();
        delete stuff;
}

void prioritize()
{
        int selection;
        cout<<"Choose a housing feature that is most important to you. \n"
        << "1 = rent ($) \n"
        << "2 = size (square feet) \n"
        << "3 = number of bedrooms \n"
        << "4 = location \n"
        << "5 = view \n"
        << "6 = smoking/non-smoking \n";
        cin>>selection;
        cin.ignore(100,'\n');
        if(selection == '1')
        {
                int rent_upperbound;
                int rent_lowerbound;
                cout<<"What is the lowest rent you want to search for?";
                cin>>rent_lowerbound;
                cin.ignore(100,'\n');
                cout<<"What is the highest rent you want to search for?";
                cin>>rent_upperbound;
                cin.ignore(100,'\n');
                for(int i=0;i<15;i++)
                {
                        if(houses1[i] != NULL && houses1[i] -> rent <= rent_upperbound && houses1[i] -> rent >= rent_upperbound);
                                Display_top(i);
                }
        }
 if(selection == '2')
        {
                int size_upperbound;
                int size_lowerbound;
                cout<<"What is the smallest square footage you want to search for?";
                cin>>size_lowerbound;
                cin.ignore(100);
                cout<<"What is the smallest square footage you want to search for?";
                cin>>size_upperbound;
                cin.ignore(100);
                for(int i=0;i<15;i++)
                {
                        if(houses1[i] != NULL && houses1[i] -> size <= size_upperbound && houses1[i] -> size >= size_lowerbound)
                                Display_top(i);
                }
        }
        if(selection == '3')
        {
                int rooms_upperbound;
                int rooms_lowerbound;
                cout<<"What is the lowest number of rooms you want to search for?";
                cin>>rooms_lowerbound;
                cin.ignore(100,'\n');
                cout<<"What is the highest number of rooms you want to search for?";
                cin>>rooms_upperbound;
                cin.ignore(100,'\n');
                for(int i=0;i<15;i++)
                {
                        if(houses1[i] != NULL && houses1[i] -> rooms <= rooms_upperbound && houses1[i] -> rooms >= rooms_lowerbound);
                                Display_top(i);
                }
        }
        if(selection == '4')
        {
                char location_select[20];
                cout<<"Enter the city you would like to search for houses in: ";
                cin>>location_select;
                cin.ignore(100,'\n');
                for(int i=0;i<15;i++)
                {
                        if(houses1[i] != NULL && strncmp(houses1[i],location_select,0) == 0);
                                Display_top(i);
                }
        }
        if(selection == '5')
        {
                char view_selection[20];
                cout<<"Enter the view you would prefer: ";
                cin>>view_selection;
                cin.ignore(100,'\n');
                for(int i=0;i<15;i++)
  {
                        if(houses1[i] != NULL && strncmp(houses1[i],view_selection,0) == 0);
                                Display_top(i);
                }
        }
        if(selection == '6')
        {
                char smoking_selection[20];
                cout<<"Enter smoking or non-smoking: ";
                cin>>smoking_selection;
                cin.ignore(100,'\n');
                for(int i=0;i<15;i++)
                {
                        if(houses1[i] != NULL && strncmp(houses1[i],view_selection,0) == 0);
                                Display_top(i);
                }
        }
}


Okay, so the error I keep getting is, throughout the prioritize function, the compiler says "Display_top was not declared in scope" and "houses1 was not declared in scope." What do I do to fix this?

***if anyone could let me know how exactly you format code on this site, that might make reading the code a lot easier. It's not working for me for some reason.
Last edited on
To format code on this website, simply select the entire code and press the '<>' button on the 'Format' bar. It should appear on your display like this:
[code]...(your code)...[/code]
, submitting the page should make it look like it does for other people.

As for your program, I presume that the 'prioritise' function is supposed to be part of your class, so change your definition in the .cpp file to this: void Housing::prioritize() { ... }

Hope this helps.
fixed it! but unfortunately, changing the definition in the .cpp file only makes the errors different and more numerous.
makes the errors different and more numerous.

Post them here.
alright. Here are the errors when I run the program as I originally had the prioritize function written:

hmwk4.Documents.cpp: In function ‘void prioritize()’:
hmwk4.Documents.cpp:40:7: error: ‘houses1’ was not declared in this scope
hmwk4.Documents.cpp:41:18: error: ‘Display_top’ was not declared in this scope
hmwk4.Documents.cpp:56:7: error: ‘houses1’ was not declared in this scope
hmwk4.Documents.cpp:57:18: error: ‘Display_top’ was not declared in this scope
hmwk4.Documents.cpp:72:7: error: ‘houses1’ was not declared in this scope
hmwk4.Documents.cpp:73:18: error: ‘Display_top’ was not declared in this scope
hmwk4.Documents.cpp:84:15: error: ‘houses1’ was not declared in this scope
hmwk4.Documents.cpp:85:18: error: ‘Display_top’ was not declared in this scope
hmwk4.Documents.cpp:96:15: error: ‘houses1’ was not declared in this scope
hmwk4.Documents.cpp:97:18: error: ‘Display_top’ was not declared in this scope
hmwk4.Documents.cpp:108:15: error: ‘houses1’ was not declared in this scope
hmwk4.Documents.cpp:108:26: error: ‘view_selection’ was not declared in this scope
hmwk4.Documents.cpp:109:18: error: ‘Display_top’ was not declared in this scope

---
Here's the error message when I make that change that was suggested:
hmwk4.Documents.cpp: In member function ‘void Housing::prioritize()’:
hmwk4.Documents.cpp:40:18: error: base operand of ‘->’ has non-pointer type ‘houses’
hmwk4.Documents.cpp:40:59: error: base operand of ‘->’ has non-pointer type ‘houses’
hmwk4.Documents.cpp:56:18: error: base operand of ‘->’ has non-pointer type ‘houses’
hmwk4.Documents.cpp:56:59: error: base operand of ‘->’ has non-pointer type ‘houses’
hmwk4.Documents.cpp:72:21: error: no match for ‘operator!=’ in ‘((Housing*)this)->Housing::houses1[i] != 0l’
hmwk4.Documents.cpp:72:21: note: candidates are:
/usr/include/c++/4.6/bits/postypes.h:223:5: note: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/stl_pair.h:214:5: note: template<class _T1, class _T2> bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/allocator.h:132:5: note: template<class _T1, class _T2> bool std::operator!=(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:137:5: note: template<class _Tp> bool std::operator!=(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/basic_string.h:2473:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2485:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2497:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/streambuf_iterator.h:200:5: note: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
hmwk4.Documents.cpp:72:40: error: base operand of ‘->’ has non-pointer type ‘houses’
hmwk4.Documents.cpp:72:83: error: base operand of ‘->’ has non-pointer type ‘houses’
hmwk4.Documents.cpp:84:43: error: cannot convert ‘houses’ to ‘const char*’ for argument ‘1’ to ‘int strncmp(const char*, const char*, size_t)’
hmwk4.Documents.cpp:96:42: error: cannot convert ‘houses’ to ‘const char*’ for argument ‘1’ to ‘int strncmp(const char*, const char*, size_t)’
hmwk4.Documents.cpp:108:26: error: ‘view_selection’ was not declared in this scope

houses1[i] -> rent As your compiler tells you operator -> is valid only on pointers or iterators. Maybe you want to use . operator

houses1[i] has a type of houses, while strcmp works with char*

Also line numbers are off. Modify your code in op post so line numbers given by compiler corresponded to lines in posted code.
Last edited on
Changed the code on the OP post; that should help clarify things.
Topic archived. No new replies allowed.