-1
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?

m8mble
  • 1,513
  • 1
  • 22
  • 30
Joe_Vj _95
  • 29
  • 3
  • 9

3 Answers3

2

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) {}
SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • This still contains one unnecessary string copy. If performance is an issue, write `car(std::string const & name, std::string & brand, ...` (remainder as you suggested. – m8mble Oct 05 '15 at 18:30
  • this is the only formatting option available in stack exchange android app 1.0.51 – Joe_Vj _95 Oct 05 '15 at 18:34
  • @SergeyA Please read [this](http://stackoverflow.com/questions/10231349/are-the-days-of-passing-const-stdstring-as-a-parameter-over). You are not correct in general... – m8mble Oct 05 '15 at 18:40
  • @m8mble I am correct in this particular case, which i never tried to generalize. – SergeyA Oct 05 '15 at 18:41
  • @SergeyA This is so strupid: You are not incorrect, if we are talking about cases were SSO doesn't apply. Otherwise you indeed are. You brought up the performance issue, and the lenghts of `name` or `brand` are not specified here. – m8mble Oct 05 '15 at 18:44
  • Nope. In this case, when SSO applies, we are dealing with the same performance. If SSO does not apply, we are dealing with better performance, and what is 10000 times more important, a heap allocation vs. no heap allocations. No-brainer, really. – SergeyA Oct 05 '15 at 18:50
  • @SergeyA: Don't you need a `std::move` in there? – Lightness Races in Orbit Oct 05 '15 at 18:57
  • @LightnessRacesinOrbit, I've been thinking avout it for several minutes. Initially I just didn't type it in a hurry - and was going to fix it the minute I noticed, but than I started thinking. Now I wonder if the compiler will (would, should, could?) optimize away the copy altogether when everything is inlined? Dunno. In real life I'd put move there, of course. – SergeyA Oct 05 '15 at 19:00
  • 1
    @SergeyA: In real life I'd take a const ref. I'm not buying into this "values everywhere" stuff just yet. – Lightness Races in Orbit Oct 05 '15 at 19:04
  • @LightnessRacesinOrbit, in real-real-real-real life here (for real!) I would do StringRef :) – SergeyA Oct 05 '15 at 19:10
2

The language does not allow the use of the syntax:

c1.car();
c2.car(" "," ",0);
c3.car(c2);

to construct an instance of the class.

You can use:

car c1; // Use the default constructor
car c2(" "," ",0); // Use the constructor with all the details.
car c3(c2);  // Use the copy constructor.

Or use assignment:

c2 = car(" "," ",0);
c3 = c2;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

In C++ constructors are called differently: You write Type name(argument1, ...). So here is what you meant to write:

int main()
{
    car c1;
    c1.disp();
    car c2(" "," ",0);
    c2.disp();
    car c3(c2);
    c3.disp();
    return 0;
}
m8mble
  • 1,513
  • 1
  • 22
  • 30