error in class

Write your question here.

hey guys am new in c++.ihve been worrking around de clock to get my class to work..to simply calculate voltage using classes but keep getting error "request for member which is a non type float" i dont knw wat am doing wrong please help.......... ihve included all my 3 separate files. the last lines are the 4 errors


my voltage.h file


[#ifndef VOLTAGE_H
#define VOLTAGE_H

class Voltage
{
public:
// default consructor
Voltage();
//overload consructor
Voltage(int,int);

//destructor
~Voltage();

//accessor function
int getResistor() ;

int getCurrent() ;

//mutator functions

void setResistor(int);

void setCurrent(int);

double calculateVoltage();



private:
//member functions

int newResistor;
int newCurrent;
};




my main,cpp file


#endif // VOLTAGE_H]

[//main.cpp
#include <iostream>
#include "voltage.h"

using namespace std;

int main()
{
int resistor;
int current;
float Voltage;
float polts;



cout<<"enter vlaue of reistor"<<endl;
cin>>resistor;
cout<<"enter current value"<<endl;
cin>>current;

Voltage polts;

polts.setResistor();
polts.setCurrent();

cout<<"voltage="<<polts.calculateVoltage()<<endl;

}]

my voltage.cpp file

[#include "voltage.h"

Voltage::Voltage()
{
newCurrent=0;
newResistor=0;
}

Voltage::Voltage(int resistor,int current)
{
newCurrent=current;
newResistor=resistor;
}

Voltage::~Voltage()
{

}

int Voltage::getResistor()
{
return newResistor;
}
int Voltage::getCurrent()
{
return newCurrent;
}



void Voltage::setResistor(int resistor)
{
newResistor=resistor;
}
void Voltage::setCurrent(int current)
{
newCurrent=current;
}

int Voltage::calculateVoltage() const
{
return newCurrent*newResistor;
}]


errors


[C:\Users\rofhiwa.eskom\Desktop\Cpp Folder\eee\main.cpp||In function 'int main()':|
C:\Users\rofhiwa.eskom\Desktop\Cpp Folder\eee\main.cpp|22|error: expected ';' before 'polts'|
C:\Users\rofhiwa.eskom\Desktop\Cpp Folder\eee\main.cpp|22|warning: statement has no effect [-Wunused-value]|
C:\Users\rofhiwa.eskom\Desktop\Cpp Folder\eee\main.cpp|24|error: request for member 'setResistor' in 'polts', which is of non-class type 'float'|
C:\Users\rofhiwa.eskom\Desktop\Cpp Folder\eee\main.cpp|25|error: request for member 'setCurrent' in 'polts', which is of non-class type 'float'|
C:\Users\rofhiwa.eskom\Desktop\Cpp Folder\eee\main.cpp|27|error: request for member 'calculateVoltage' in 'polts', which is of non-class type 'float'|
||=== Build finished: 4 errors, 1 warnings (0 minutes, 0 seconds) ===|
][/output]
Last edited on
You have two variables polts in main():

float polts;

Voltage polts;

just name them differently


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
sori still dnt get coz polts is my object and when i remove my declaration of float polts it says polts was not declared....am i creating my object in a wrong way maybe
Last edited on
dunot remov d declaration of float polts, just change d variable name to something else
Last edited on
Voltage.h
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
#ifndef VOLTAGE_H
#define VOLTAGE_H



class Voltage {
public:

// default consructor
    Voltage();
//overload consructor
    Voltage(int,int);
//accessor function
    int getResistor() ;
    int getCurrent() ;
//mutator functions
    void setResistor(int);
    void setCurrent(int);
    double calculateVoltage();

private:
//member functions
    int newResistor;
    int newCurrent;

};

#endif // VOLTAGE_H


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "Voltage.h"

using namespace std;

int main()
{
    Voltage volts;

    int resistor = 0, current = 0;

    cout<<"enter vlaue of reistor"<<endl;
    cin>>resistor;
    cout<<"enter current value"<<endl;
    cin>>current;

    volts.setResistor(resistor);
    volts.setCurrent(current);

    cout<<"voltage="<<volts.calculateVoltage()<<endl;

}


Voltage.cpp
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
#include "Voltage.h"

Voltage::Voltage()
{
    newCurrent=0;
    newResistor=0;
}

Voltage::Voltage(int resistor,int current)
{
    newCurrent=current;
    newResistor=resistor;
}

int Voltage::getResistor()
{
    return newResistor;
}
int Voltage::getCurrent()
{
    return newCurrent;
}

void Voltage::setResistor(int resistor)
{
    newResistor=resistor;
}
void Voltage::setCurrent(int current)
{
    newCurrent=current;
}

double Voltage::calculateVoltage()
{
    return newCurrent*newResistor;
}


The above code should run on your PC, things to note,
1) polts was of class type, and defining it as a float would then be an error!
Last edited on
It may help to look @ your code in this state if your {implementation and interface part is giving u problems}!

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
#include <iostream>

using namespace std;

class Voltage {
public:

// default consructor
    Voltage();
//overload consructor
    Voltage(int,int);
//accessor function
    int getResistor() ;

    int getCurrent() ;

//mutator functions

    void setResistor(int);

    void setCurrent(int);

    double calculateVoltage();

private:
//member functions

    int newResistor;
    int newCurrent;
};


Voltage::Voltage()
{
    newCurrent=0;
    newResistor=0;
}

Voltage::Voltage(int resistor,int current)
{
    newCurrent=current;
    newResistor=resistor;
}

int Voltage::getResistor()
{
    return newResistor;
}
int Voltage::getCurrent()
{
    return newCurrent;
}

void Voltage::setResistor(int resistor)
{
    newResistor=resistor;
}
void Voltage::setCurrent(int current)
{
    newCurrent=current;
}

double Voltage::calculateVoltage()
{
    return newCurrent*newResistor;
}


int main()
{
    Voltage volts;

    int resistor = 0, current = 0;

    cout<<"enter vlaue of reistor"<<endl;
    cin>>resistor;
    cout<<"enter current value"<<endl;
    cin>>current;

    volts.setResistor(resistor);
    volts.setCurrent(current);

    cout<<"voltage="<<volts.calculateVoltage()<<endl;

}
Last edited on
the code runs perfactly well in a single but in serparate files it doesnt work
Topic archived. No new replies allowed.