Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
Hello all,

I just came from programming class (c++). The program i wrote won't compile for reasons unknown (compiling from Darwin). I've just started using classes in c++ so i am kind of new to this, but my teacher almost swears that my code would funtion on a windows machine, so it has to be the Os. here's the the code, really simple:

Code:
#include<iostream>
using namespace std;

class BombaGas{
	private:
		float litros, prexlit, cant, tot;
		
	public:
		BombaGas();
		BombaGas(float,float);
		
		void despliega();
		float despacha(float);
		
};
	BombaGas:BombaGas(){
	litros=900;
	prexlit=7.5;
	cant=0;
	tot=0.0;
	}
	
	BombaGas:BombaGas(float lit, float pre){
	litros=lit;
	prexlit=pre;
	cant=0;
	tot=0.0;
	}
	
	void despliega(){
	cout<<"Cantidad de Gas"<<litros<<endl;
	cout<<prexlit<< "precio por litro"endl;
	}
	
	float despacha(float x) {
	litros=litros-x;
	tot=(prexlit*x);
	return tot;
	}
	
int main(){

char op;
float ven;
float total;
BombaGas autombile, chevy(500,5) ;

do{
	cout<<"#$#$#$ MENU #$#$#$"<<endl;
	cout<<"Ver contenido Actual			1."<<endl;
	cout<<"Compra de Gas				2."<<endl;
	cout<<"Salir						3."<<endl;
	cin>>op;
	
	switch(op){
		case'1':	automobile.despliega();
			  	chevy.despliega();
		break;
		case'2':	cout<<"Cuantos litros quieres?"<<endl;
				cin>>cant;
				total=despacha(cant);
				cout<<"Se cobra"<<total;	
		break;
		case'3':
		break;
		}
 while(op!='3');

return 0;
}

these are the error messages I am getting
Code:
bombagas.cpp: In function `void despliega()':
bombagas.cpp:33: error: 'litros' was not declared in this scope
bombagas.cpp:34: error: 'prexlit' was not declared in this scope
bombagas.cpp:34: error: expected `;' before "endl"
bombagas.cpp: In function `float despacha(float)':
bombagas.cpp:38: error: 'litros' was not declared in this scope
bombagas.cpp:39: error: 'tot' was not declared in this scope
bombagas.cpp:39: error: 'prexlit' was not declared in this scope
bombagas.cpp: In function `int main(int, char**)':
bombagas.cpp:58: error: 'automobile' was not declared in this scope
bombagas.cpp:62: error: 'cant' was not declared in this scope
bombagas.cpp:72: error: expected `while' at end of input
bombagas.cpp:72: error: expected `(' at end of input
bombagas.cpp:72: error: expected primary-expression at end of input
bombagas.cpp:72: error: expected `)' at end of input
bombagas.cpp:72: error: expected `;' at end of input
bombagas.cpp:72: error: expected `}' at end of input

I am guessing classes have a different decalration in unix.Any help?
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
Hello all,

I just came from programming class (c++). The program i wrote won't compile for reasons unknown (compiling from Darwin). I've just started using classes in c++ so i am kind of new to this, but my teacher almost swears that my code would funtion on a windows machine, so it has to be the Os. here's the the code, really simple:

Code:
#include<iostream>
using namespace std;

class BombaGas{
    private:
        float litros, prexlit, cant, tot;
        
    public:
        BombaGas();
        BombaGas(float,float);
        
        void despliega();
        float despacha(float);
        
};
    BombaGas:BombaGas(){
    litros=900;
    prexlit=7.5;
    cant=0;
    tot=0.0;
    }
    
    BombaGas:BombaGas(float lit, float pre){
    litros=lit;
    prexlit=pre;
    cant=0;
    tot=0.0;
    }
    
    void despliega(){
    cout<<"Cantidad de Gas"<<litros<<endl;
    cout<<prexlit<< "precio por litro"endl;
    }
    
    float despacha(float x) {
    litros=litros-x;
    tot=(prexlit*x);
    return tot;
    }
    
int main(){

char op;
float ven;
float total;
BombaGas autombile, chevy(500,5) ;

do{
    cout<<"#$#$#$ MENU #$#$#$"<<endl;
    cout<<"Ver contenido Actual            1."<<endl;
    cout<<"Compra de Gas                2."<<endl;
    cout<<"Salir                        3."<<endl;
    cin>>op;
    
    switch(op){
        case'1':    automobile.despliega();
                  chevy.despliega();
        break;
        case'2':    cout<<"Cuantos litros quieres?"<<endl;
                cin>>cant;
                total=despacha(cant);
                cout<<"Se cobra"<<total;    
        break;
        case'3':
        break;
        }
 while(op!='3');

return 0;
}
these are the error messages I am getting
Code:
bombagas.cpp: In function `void despliega()':
bombagas.cpp:33: error: 'litros' was not declared in this scope
bombagas.cpp:34: error: 'prexlit' was not declared in this scope
bombagas.cpp:34: error: expected `;' before "endl"
bombagas.cpp: In function `float despacha(float)':
bombagas.cpp:38: error: 'litros' was not declared in this scope
bombagas.cpp:39: error: 'tot' was not declared in this scope
bombagas.cpp:39: error: 'prexlit' was not declared in this scope
bombagas.cpp: In function `int main(int, char**)':
bombagas.cpp:58: error: 'automobile' was not declared in this scope
bombagas.cpp:62: error: 'cant' was not declared in this scope
bombagas.cpp:72: error: expected `while' at end of input
bombagas.cpp:72: error: expected `(' at end of input
bombagas.cpp:72: error: expected primary-expression at end of input
bombagas.cpp:72: error: expected `)' at end of input
bombagas.cpp:72: error: expected `;' at end of input
bombagas.cpp:72: error: expected `}' at end of input
I am guessing classes have a different decalration in unix.Any help?
Your problem is the declaration of despliega() and despacha(). They should be declared as belonging to the BombaGas class:
Code:
BombaGas::despliega()
BombaGas::despacha()
In despliega(), you're missing a << in your output statement between "precio por litro" and endl;.
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
First mistake:

In C++ you use the syntax <Class>::<Method> e.g. BombaGas::BombaGas() to call a member function. In your code you had a single colon BombaGas:BombaGas()
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.