Originally Posted by
stampede
class Car();
class Car();
To copy to clipboard, switch view to plain text mode
This should result in compilation error.
I guesss that depends on compiler, in my case it doesn't. But you are right, with the "()" you are making compiler think it is a function which returns a class. I've mixed up the declaration and definition in my Car class and you are right again it does not compile. Here is a corrected example:
car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
class Car
{
public:
Car();
Car(int a, int b);
};
#endif // CAR_H
#ifndef CAR_H
#define CAR_H
#include <iostream>
class Car
{
public:
Car();
Car(int a, int b);
};
#endif // CAR_H
To copy to clipboard, switch view to plain text mode
car.cpp
#include "car.h"
Car::Car()
{
std::cout<<"default constructor\n";
}
Car::Car(int a, int b){
std::cout<<"constructor with 2 params\n";
}
#include "car.h"
Car::Car()
{
std::cout<<"default constructor\n";
}
Car::Car(int a, int b){
std::cout<<"constructor with 2 params\n";
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QCoreApplication>
#include <iostream>
#include <car.h>
int main(int argc, char *argv[])
{
std::cout<<"starting...\n";
Car Fiat;
Car Audi;
std::cout<<"ending...\n";
return a.exec();
}
#include <QCoreApplication>
#include <iostream>
#include <car.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout<<"starting...\n";
Car Fiat;
Car Audi;
std::cout<<"ending...\n";
return a.exec();
}
To copy to clipboard, switch view to plain text mode
However what I want was already answered at the beginning, that is, create a pointer and crate instance later. My apologies for misleading question and thanks for your help.
Bookmarks