VS compiling error when passing arguments to c++ constructor - c++

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;
};

Related

constexpr initializers and enum template arguments

I'm noticing something strange in my code when I use constexpr initializers and enum template argumets. For example, the following code will compile:
enum class TestEnum1 {
Val1,
Val2
};
template <TestEnum1 EnumVal> class TestClass1 {
public:
constexpr TestClass1() {
}
};
class TestClass2 {
public:
TestClass1<TestEnum1::Val1> TestObject = TestClass1<TestEnum1::Val1>();
};
int main() {
TestClass2 testObj;
}
while the following will not:
enum class TestEnum1 {
Val1,
Val2
};
template <typename T, TestEnum1 EnumVal> class TestClass1 {
public:
constexpr TestClass1() {
}
};
class TestClass2 {
public:
TestClass1<int, TestEnum1::Val1> TestObject = TestClass1<int, TestEnum1::Val1>();
};
int main() {
TestClass2 testObj;
}
note that I only added another template argument before the existing one. The error message is something like:
error: 'enum class TestEnum1' is not a class or a namespace
error: expected ';' at end of member declaration
error: expected unqualified-id before '>' token
error: expected unqualified-id before ')' token
error: template argument 1 is invalid
Glad if someone would explain why or at least solve the problem. I'm using g++5.3 with --std=c++11 and windows 10.
EDIT: I just tested enum, under which circumstance errors still occurred, but with the first item on the error list removed. Tested int and everything went fine.

template class instantiation in another class in c++

I have a template class that when I instantiate in main doesn't have any issues but when i try instantiating the same in another class is giving issues. can someone please enlighten me on the solution for this
#include<iostream>
#include<string>
using namespace std;
template <class T>
class property {
public:
property(string name)
{
propertyName= name;
}
private:
T item;
string propertyName;
};
main()
{
property<int> myIntProperty("myIntProperty");
}
the above code compiles with out any issue.
but
#include<iostream>
#include<string>
using namespace std;
template <class T>
class property {
public:
property(string name)
{
propertyName= name;
}
private:
T item;
string propertyName;
};
class propertyHolder
{
property<int> myIntProperty("myIntProperty");
};
this code is not getting compiled.
giving me error like
main.cpp|19|error: expected identifier before string constant|
main.cpp|19|error: expected ',' or '...' before string constant|
Thanks,
Harish
property<int> myIntProperty("myIntProperty");
This is a function declaration, so it expects you to insert a default argument after identifying it, like string s = "myIntProperty".
Perhaps you want to initialize an object called myIntProperty,
property<int> myIntProperty {"myIntProperty"};
This can be done in C++11, but you can also initialize it in the constructor initializer list,
// Header
class propertyHolder {
public:
propertyHolder( string s );
private:
property<int> myIntProperty;
};
// Source
propertyHolder::propertyHolder( string s ) :
myIntProperty( s )
{
}
You wanted to declare field in class propertyHandler. That syntax is not working because you cannot declare a field and assing it value at the same spot.
You can delcare it, and initialise in constructor:
property<int> myIntProperty;
propertyHolder(): myIntProperty("name") {}
or with c++11 syntax:
property<int> myIntProperty{"name"};
or declare it static, and them declare like that:
static property<int> myIntProperty;
and after class declaration:
property<int> propertyHolder::myIntProperty("name");

Forward declaration inside a namespace

I've encountered a strange problem when trying to compile my code in Visual Studio (2010); here's an isolated (and simplified) example:
class A
{
public:
enum {
VALUE = 0
};
};
namespace ns
{
class A;
class B
{
public:
B(int val = ::A::VALUE) // this line
{}
};
class A : public ::A
{
public:
};
}
This gives the following error: error C2027: use of undefined type 'ns::A'. Is it a bug in VS or am I doing something wrong?
Update: this appears to be a bug specific to Visual Studio. Here's a workaround suggested on Microsoft Connect:
class B
{
public:
typedef ::A A1;
B(int val = A1::VALUE)
{}
};
This is a bug in VC10. Your ::A class name is fully qualified, and the definition of A in the global namespace is visible to the compiler. Besides, GCC 4.7.2 compiles this without problems.

member object constructor and enum

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

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;
}