Help!
I’m new to C++…
how can I fix this header file?
#pragma once
class MyCls2
{
private:
int _i, _j;
public:
MyCls2(int i, int j) : _i(i),
_j(j)
MyCls2(); // error: expected a '{'
~MyCls2(void);
};
This is the error in MS VC 2010:
error: expected a ‘{‘
Thanks for the help, I got what I want now:
.h:
#pragma once
class MyCls2
{
private:
int _i, _j;
public:
MyCls2(int i, int j) ;
MyCls2();
~MyCls2(void);
};
.cpp:
#include "StdAfx.h"
#include "MyCls2.h"
MyCls2::MyCls2()
{
}
MyCls2::MyCls2(int i, int j) : _i(i),
_j(j)
{
}
MyCls2::~MyCls2(void)
{
}
You’re missing the function body in your definition of the
MyCls2constructor that takes two ints.Think of the initializer list as being part of the constructor itself (its definition, not its declaration). You can’t have part of a function’s definition somewhere, and another part elsewhere.
If you want the initializer list in the header, you need the rest of that definition (the constructor body) in the header too, as above.
If you don’t want the definition in the header, don’t put the initializer list in the header, put it in the implementation file.