member object constructor and enum - c++

Why does this not compile?
File.hpp
class CTest
{
public:
enum enumTest { EN_TEST };
//constructor:
CTest(enumTest f_en);
};
AnotherFile.hpp
#include "File.hpp"
class CAnotherTest
{
public:
CTest obj_Test(CTest::EN_TEST);
};
Visual Studio says: error C2061: syntax error : identifier 'EN_TEST'
armcc compiler says: error: #757: constant "CTest::EN_TEST" is not a type name
Thanks, Mirco

Because,
CTest obj_Test(CTest::EN_TEST);
is evaluated as a function named obj_Test. Now it should have argument as a type, however, CTest::EN_TEST is a value, not a type.
If it's intended that obj_Test an object then you have pass CTest::EN_TEST to it in the constructor:
class CAnotherTest
{
public:
CAnotherTest () : obj_Test(CTest::EN_TEST) {}
};

Because your syntax for CAnotherTest is wrong. Perhaps you mean something like this?
class CAnotherTest
{
public:
// Constructor vvv Initialise member variable
CAnotherTest() : obj_Test(CTest::EN_TEST) {}
// Member variable
CTest obj_Test;
};

You cannot initialize like that. In-class initialization can be done for only static const integral type.
Use initialization-list in the constructor, as:
class CAnotherTest
{
public:
CTest obj_Test; //member declaration. no initialization here
static const int value = 100; //OK. static const integral type!
CAnotherTest() : obj_Test(CTest::EN_TEST) {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^ its called initialization-list
};
const int CAnotherTest::value; //definition goes to .cpp file

Related

Why can't access to enum here?

Here's my code/namespace:
namespace myNamespace {
enum MyType {
ASD1,
ASD2,
ASD3
};
struct MyClass {
MyType mMyType;
MyClass(MyType myType = MyType::ASD1) : mMyType(myType) {
}
};
}
Now if I try, within another struct, this code:
struct X
{
myNamespace::MyClass *pMyClass1 = new myNamespace::MyClass(myNamespace::MyType::ASD2);
};
it works perfectly, but if I try this:
struct X
{
myNamespace::MyClass mMyClass1(myNamespace::MyType::ASD2);
};
it says 'myNamespace::MyType::ASD2' is not a type.
Since its all declared before, why this?
Inside class, you might use {..} or = .. syntax, not (..):
struct Test {
myNamespace::MyClass mMyClass1{myNamespace::MyType::ASD2};
// myNamespace::MyClass mMyClass1 = myNamespace::MyClass{myNamespace::MyType::ASD2};
};
You have to use the brace-or-equal-initializer. You may not use an initializer in parentheses without the sign =.
From the C++ Standard (12.2 Class members)
member-declarator:
declarator virt-specifier-seqopt pure-specifieropt
declarator brace-or-equal-initializeropt
identifieropt attribute-specifier-seqopt : constant-expression
For example
myNamespace::MyClass mMyClass1 { myNamespace::MyType::ASD2 };
or
myNamespace::MyClass mMyClass1 = myNamespace::MyType::ASD2;
The last initialization is valid because the constructor is a conversion constructor

VS compiling error when passing arguments to c++ constructor

I'm trying to pass two arguments to a constructor:
class CTest1
{
public:
CTest1(const int i8BitImageID, const int i256BitImageID) : m_i8BitImageID(i8BitImageID), m_i256BitImageID(i256BitImageID) {};
private:
int m_i8BitImageID;
int m_i256BitImageID;
};
#define BITMAP_1_ID 1
#define BITMAP_2_ID 2
class CTest2
{
public:
CTest1 test1(BITMAP_1_ID, BITMAP_2_ID); // Compile error here
};
When I compile this (using Visual Studio 2017), the line where I declare "test1" results in a "C2059: syntax error: 'constant'" error. I've tried with an without "const" in the definition of the constructor.
Thanks!
Default member initializer only works with brace or equals initializer. e.g.
class CTest2
{
public:
CTest1 test1 = CTest1(BITMAP_1_ID, BITMAP_2_ID);
CTest1 test2 {BITMAP_1_ID, BITMAP_2_ID};
};
Or you could use member initializer list.
class CTest2
{
public:
CTest2() : test1(BITMAP_1_ID, BITMAP_2_ID) {}
CTest1 test1;
};

Qt C++ - Creation QList<Myclass> - is not a valid template type argument for parameter 'T

I have just started with C++ Qt and I wrote the following .h file: The .cpp file contains only empty constructors and destructors.
#include <QList>
class XML_Files
{
public:
XML_Files();
~XML_Files();
protected:
QList<Myclass> m_Xmls;
};
class Myclass
{
public:
Myclass();
~Myclass();
protected:
int a;
};
but I keep getting the following errors:
error C2065: 'Myclass': undeclared identifier
error C2923: 'QList': 'Myclass' is not a valid template type argument for parameter 'T'
What do I have to do to declare a Qlist with my own data type?
You can't use the name MyClass until the compiler knows about it. Since you do not declare MyClass until after XML_Files you cannot use it's name in XML_Files.
The simplest solution here is to just change the order of the declarations and declare MyClass before XML_Files.
The easy way to fix this, is to turn the order of both classes. However there is a second solution, if this is not desired or possible:
You may declare Myclass before defining it. Then compilation will succeed.
#include <QList>
class Myclass;
class XML_Files
{
public:
XML_Files();
~XML_Files();
protected:
QList<Myclass> m_Xmls;
};
class Myclass
{
// ...
};

Using a member of the defined type with the TI compiler

I have a class that looks something like this:
class In {
public:
struct Member{
In name;
};
In() {}
private:
static const int aCapacity = 16;
static const int oCapacity = 16;
};
When I attempt to compile it I get an error: error #71: incomplete type is not allowed
This code does compile with the Microsoft compiler. Wondering if anyone knows of a way to make this work for TI?
AFAIK, TI uses GCC 4.8.3.
BTW, the actual class is a template, but I am pretty sure that is not an issue here.
This should not compile : the compiler is unable to deduce the layout of Member since it hasn't parsed the entire class In yet.
Just declare the nested struct, and define it after the definition of In :
class In {
public:
struct Member;
In() {}
private:
static const int aCapacity = 16;
static const int oCapacity = 16;
};
struct In::Member{
In name;
};

How to : Static variable in inherited class

class B {
public:
static int a;
};
class C:B {
};
I want to use a variable through any inherited classes but it has problem when I declare a.
B::B() {
a=1;
};
Do I do it right ?
Thanks for reading and waiting for your comments.
// I miss semicolons which is not the error I'm talking .
// This is an error when I try to delcare
class GameState {
public:
static int a = 1;
//...
};
Error 7 error C2864: 'CGameState::a' : only static const integral data members can be initialized within a class d:\my dropbox\work\#today\gdimario\gdimario\gamestate.h 18
I try to write a simple question which shows the problem I want instead of pasting my whole code.
You can use it directly like you did from both the derived and base class.
Perhaps your error is that you don't have semicolons at the end of your class declarations?
class B {
public:
static int a;
};
class C:B {
};
If you want to call it from an instance of C then you need to use public inheritance: (If nothing is specified private inheritance is assumed)
class C : public B {
};
To initialize a you need to do this (typically at the top of your corresponding .CPP file):
int B::a = 3;
You need to write in a CPP file:
int B::a;
And add the semicolons that Brad suggests. (Did you even compile your code? What did the compiler say?)
i think you ll get linker error.
since you have not defined of the static variable in the .cpp file.
e.g.
//hearer file
class X{
public : static int a ;
}
//impl file
int X::a(0);
....or...
For integral type you can also defined static variables when they are declared like:
class X{
public : static int a = 0;
}