Search code examples
c++classprogramming-languages

invalid use of `class car'


using namespace std;

class car
{
private:
    string name;
    string brand;
    int tspeed;
public:
    car();
    car(string name1,string brand1,int ip);
    car(car &ref);
    ~car();
    void disp();
};

car::car()
{
    cout<<"default constructor invoked\n";
}

car::car(string name1, string brand1, int sp)
{
    name=name1;
    brand=brand1;
    tspeed=sp;
    cout<<"parameterised constructor invoked\n";
}

car::car(car &ref)
{
    name=ref.name;
    brand=ref.brand;
    tspeed=ref.tspeed;
    cout<<"copy constructor invoked\n";
}

car::~car()
{
    cout<<"destructor invoked\n";
}

void car::disp()
{
    cout<<"enter the name\n";
    cin>>name;
    cout<<"entr the brand\n";
    cin>>brand;
    cout<<"enter the top speed\n";
    cin>>tspeed;
    cout<<"name:"<<name<<endl;
    cout<<"brand:"<<brand<<endl;
    cout<<"top speed:"<<tspeed<<endl;
}

int main()
{
    car c1,c2,c3;
    c1.car();
    c1.disp();
    c2.car(" "," ",0);
    c2.disp();
    c3.car(c2);
    c3.disp();
    return 0;
}

--------------------Configuration: mingw5 - CUI Release, Builder Type: MinGW--------------------

Checking file dependency...

Compiling C:\Users\Joe\Documents\C-Free\Temp\Untitled1.cpp...

[Error] C:\Users\Joe\Documents\C-Free\Temp\Untitled1.cpp:55: error: invalid use of `class car'

[Error] C:\Users\Joe\Documents\C-Free\Temp\Untitled1.cpp:56: error: `c1' was not declared in this scope

[Error] C:\Users\Joe\Documents\C-Free\Temp\Untitled1.cpp:57: error: `c2' was not declared in this scope

[Error] C:\Users\Joe\Documents\C-Free\Temp\Untitled1.cpp:59: error: `c3' was not declared in this scope

[Warning] C:\Users\Joe\Documents\C-Free\Temp\Untitled1.cpp:62:2: warning: no newline at end of file

Complete Make Untitled1: 4 error(s), 1 warning(s)

what is the error with invalid use of class car?


Solution

  • You are incorrectly using Car constructor here. Instead, your code should be following:

    car c2(" "," ",0);
    

    Your code also has a performance problem - you are unneccessary creating string copies. You should have following signature for your constructor:

    car(std::string name, std::string brand, int tspeed) : name(name), brand(brand), tspeed(tspeed) {}