0

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

Harish
  • 23
  • 5

2 Answers2

2
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 )
{
}
aslg
  • 1,966
  • 2
  • 15
  • 20
  • Thanks that solved the issue :). can you explain a bit more or point me to documentation where I can find what went wrong in the previous case? – Harish Oct 04 '15 at 11:47
  • @Harish [Why C++11 in-class initializer cannot use parentheses?](http://stackoverflow.com/q/24836526/3953764) – Piotr Skotnicki Oct 04 '15 at 11:48
  • This would lead to the "[Most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse)". – aslg Oct 04 '15 at 11:51
1

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");
Lapshin Dmitry
  • 1,084
  • 9
  • 28
  • *"That syntax is not working because you cannot declare a field and assing it value at the same spot."* You cannot assign before initialisation, yes. But as your own answer shows, C++11 does allow initialisation in the class declaration (even if you incorrectly call it "assign"). – Christian Hackl Oct 04 '15 at 14:48
  • @ChristianHackl my words could be a little be incorrect, but as far I can tell I showed right code. I am trying my best. If you can tell me correct names of actions I can edit my answer with them, because I have learned C++ not in English terminology. – Lapshin Dmitry Oct 04 '15 at 14:51
  • Your code is correct, of course. But you should generally try to distinguish a bit more between assignment and initialisation. They are different things. – Christian Hackl Oct 04 '15 at 14:54
  • 1
    @ChristianHackl I understand, I just can't say it in English properly on 100%. But I am trying a lot) – Lapshin Dmitry Oct 04 '15 at 14:55