Scope problems

Im having a problem with scope errors. Im trying to use a function from my class and even with including the class file it isnt working.
proj1.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "food.h"
#include <iostream>
#include <iomanip>
#include <string>

int main () {
do{
    menu();
    cout << "Enter a number between 0 and 8";
    cin >>  loop;
    
        switch (loop){
        case 1:
            print (lunch);
            cout << endl << endl;
            break;
   ..........
    return 0;
    }


food.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using namespace std;
#ifndef _FOOD_H_
#define _FOOD_H_
#include <string>
#include <iostream>
class food
{
    public:
       void print(const food& a);
        food choose (food& f1, food& f2, food& f3, food& f4, food& f5, food& f6);
    private:
	    string name;
	    double protein;
	    double fiber;
	    double fat;
	    double calories;
}
#endif // _FOOD_H_ 


food.cc
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
#include "food.h"
#include <string>
#include <iostream>

 // *******************************************************************************
 print (food& a){
    cout<< a.name << endl;
	cout << "protein: " << a.protein << endl;
	cout << "fiber:" << a.fiber << endl;
	cout << "fat:" << a.fat << endl;
	cout << "calories:" << a.calories << endl;
	}
// *******************************************************************************
 choose (food& f1, food& f2, food& f3, food& f4, food& f5, food& f6){
 int temp;
 cout << "choose a food" << endl << endl << "1";
 print (f1);
 cout << endl << endl << "2";
 print (f2);
 cout << endl << endl << "3";
 print (f3);
 cout << endl << endl << "4";
 print (f4);
 cout << endl << endl << "5";
 print (f5);
 cout << endl << endl << "6";
 print (f6);
 cout << endl << endl;
 cin >> "enter a number from 1-6" >> temp;
 switch (temp){
        case 1:
            return f1;
        case 2:
            return f2;
        case 3:
            return f3;
        case 4:
            return f4;
        case 5:
            return f5;
        case 6:
            return f6;
     }
  }




the errors specifically i am getting is
error: ‘print’ was not declared in this scope
error: ‘choose’ was not declared in this scope
It looks like you want to include food.cc not food.h in your main file...
1
2
//proj1.cc (1)
#include "food.cc" //food.h is included in food.cc already, so you don't need both here  


FYI: classes end with a ;
1
2
3
//food.h (17)
}; // <- need the ; here 
#endif // _FOOD_H_  

Last edited on
It looks like you want to include food.cc not food.h in your main file...


No, that isn't necessary and in fact it's generally not a good thing to do...

First, you do need a ; at the end of your class as stated above.
Second, your functions are missing their scope operators...
e.g.:
food::print(food& a)
Third, your functions seem like they are supposed to be global functions, not members anyway.
Thanks guys,
After getting this bug solved now im getting another one in multiple areas.
error: expected initializer before ‘operator’

Food.cc
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
// *******************************************************************************
 food food operator + (const food& a, const food& b){
 	string nme = a.name;
	double cpro = a.protein + b.protein;
	double cfib = a.fiber + b.fiber;
	double cft = a.fat + b.fat;
	double ccal = a.calories + b.calories;
	food temp( nme, cpro, cfib, cft, ccal);
	return temp;
  }
 // *******************************************************************************
 food food operator - ( food& a, const food& b){
    string st= a.name;
    int del = st.find(b.name);
    del = del -1;
    string st2 = b.name;
    int nxt = st2.length();
    st.erase(del,nxt);
	double cpro = a.protein - b.protein;
	double cfib = a.fiber - b.fiber;
	double cft = a.fat - b.fat;
	double ccal = a.calories - b.calories;
	food temp( st, cpro, cfib, cft, ccal);
	return temp;
 }
 // *******************************************************************************
 food food operator / (const food& a, int div){
 	string nme = a.name;
	double cpro = a.protein / div;
	double cfib = a.fiber / div;
	double cft = a.fat / div;
	double ccal = a.calories / div;
	food temp( nme, cpro, cfib, cft, ccal);
	return temp;
   }
 // *******************************************************************************
 food food operator * (food& a, const int mul){
 	string nme = a.name;
	double cpro = a.protein * mul;
	double cfib = a.fiber * mul;
	double cft = a.fat * mul;
	double ccal = a.calories * mul;
	food temp( nme, cpro, cfib, cft, ccal);
	return temp;
   }
 // *******************************************************************************
 bool food :: operator < (food& a, food& b){
   return (a.calories < b.calories);
   }
 // *******************************************************************************
bool  food :: operator > (food& a, food& b){
   return (a.calories > b.calories);
   }
// *******************************************************************************
 bool food operator == (const food& a,const food& b){
  return bool (a.calories == b.calories);
  }
 // ******************************************************************************* 
 food food operator = (const food& b){
      if(this == &b)
        return *this;
	}


food.h
1
2
3
4
5
6
7
8
9
10
11
12
class food
{
    public:
        // overload operators
        friend food operator + (const food& a, const food& b);
        friend food operator - (const food& a, const food& b);
        friend food operator / (const food& a, const int div);
        friend food operator * (food& a, const int mul);
        friend bool operator < (food& a, food& b);
        friend bool operator > (food& a, food& b);
        friend bool operator == (const food& a,const food& b);
        food & operator = (const food& b);


thanks again
Topic archived. No new replies allowed.