2014-01-19 4 views
0

Я попытался реализовать Properties в C++. Я не знаю, почему, но если я хочу скомпилировать свой код, существует довольно много ошибок. Основная идея заключалась в том, что класс шаблона и конструктор tamplate предоставят информацию о требованиях.C++ Template Class with Template Constructor

Я был бы признателен, если бы кто-нибудь мог мне помочь!

Компиляция Сообщение:

[email protected] ~/dev/property $ gcc -std=c++0x -o PropertyTest2 PropertyTest2.cpp 
PropertyTest2.cpp:22:16: error: expected ‘;’ at end of member declaration 
PropertyTest2.cpp:22:19: error: expected unqualified-id before ‘<’ token 
PropertyTest2.cpp: In function ‘int main()’: 
PropertyTest2.cpp:34:20: error: use of deleted function ‘PropertyTestClass::PropertyTestClass()’ 
PropertyTest2.cpp:8:7: error: ‘PropertyTestClass::PropertyTestClass()’ is implicitly deleted because the default definition would be ill-formed: 
PropertyTest2.cpp:8:7: error: no matching function for call to ‘Property<int>::Property()’ 
PropertyTest2.cpp:8:7: note: candidates are: 
Property4.cpp:21:2: note: template<int (** G)(), void (** S)(int&)> Property::Property() 
Property4.cpp:6:7: note: constexpr Property<int>::Property(const Property<int>&) 
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided 
Property4.cpp:6:7: note: constexpr Property<int>::Property(Property<int>&&) 
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided 
PropertyTest2.cpp:38:20: error: no matching function for call to ‘Property<int>::Set(int)’ 
PropertyTest2.cpp:38:20: note: candidate is: 
Property4.cpp:30:7: note: void Property<T>::Set(T&) [with T = int] 
Property4.cpp:30:7: note: no known conversion for argument 1 from ‘int’ to ‘int&’ 

недвижимости класса (Property.cpp)

#ifndef __PROPERTY_FH__ 
#define __PROPERTY_FH__ 


template <class T> 
class Property { 
private: 
    typedef T (*TGetter)(void); 
    typedef void (*TSetter)(T &); 

    TGetter Getter; 
    TSetter Setter; 


public: 
    typedef T type; 

    template<TGetter *G, 
      TSetter *S 
      > 
    Property() { 
     this->Getter = G; 
     this->Setter = S; 
    } 

    T Get(void) { 
     return (this->Getter)(); 
    } 

    void Set(T &value) { 
     (this->Setter)(value); 
    } 
}; 

#endif 

Тестирование файла (PropertyTest.cpp):

#ifndef __PROPERTY_TEST_FH__ 
#define __PROPERTY_TEST_FH__ 

#include <iostream> 
#include "Property.cpp" 


class PropertyTestClass { 
private: 
    // ReadWrite Property for age 
    int _age; 
    int AgeGetter(void) { 
     return this->_age; 
    } 
    void AgeSetter(int &value) { 
     this->_age = value; 
    } 


public: 
    // ReadWrite Property for age 
    Property<int> age<&PropertyTestClass::AgeGetter, &PropertyTestClass::AgeSetter>; 
}; 

#endif 


/** 
* Program Entry 
**/ 
int main() { 
    std::cout << "Property Test Programm\n\n"; 

    PropertyTestClass propTest; 


    std::cout << "ReadWrite Property for age\n"; 
    propTest.age.Set(5); 
    std::cout << propTest.age.Get() << "\n"; 


    return 0; 
} 
+0

Во-первых, '' TGetter' и TSetter' не определены в 'собственности. cpp'. Во-вторых, этот файл не должен иметь расширение '.cpp'. Ваш компилятор/IDE может попытаться скомпилировать его самостоятельно. – juanchopanza

+1

Возможный дубликат http://stackoverflow.com/questions/2861839/can-the-template-parameters-of-a-constructor-be-explicitly-specified – Constructor

+0

Не используйте '(void)' для функций нулевых параметров, просто используйте '()'. '(void)' существует только для совместимости с C. – Manu343726

ответ

0

Ok, на этот раз фиксируется все проблемы в вашем коде.

Property.cpp:

#ifndef __PROPERTY_FH__ 
#define __PROPERTY_FH__ 

#include <boost/function.hpp> 

template <class T> 
class Property { 
private: 

    typedef boost::function <T()> TGetter; 
    typedef boost::function <void(const T&)> TSetter; 

    TGetter Getter; 
    TSetter Setter; 


public: 
    typedef T type; 

    Property(TGetter G, TSetter S) { 
     this->Getter = G; 
     this->Setter = S; 
    } 

    T Get(void) { 
     return (this->Getter)(); 
    } 

    void Set(const T &value) { 
     (this->Setter)(value); 
    } 
}; 

#endif 

PropertyTests.cpp:

#ifndef __PROPERTY_TEST_FH__ 
#define __PROPERTY_TEST_FH__ 

#include <iostream> 
#include <boost/bind.hpp> 
#include "Property.cpp" 


class PropertyTestClass { 
private: 
    // ReadWrite Property for age 
    int _age; 
    int AgeGetter() { 
     return this->_age; 
    } 
    void AgeSetter(const int &value) { 
     this->_age = value; 
    } 


public: 
    // ReadWrite Property for age 
    Property<int> age; 
    PropertyTestClass() : age(
     boost::bind(&PropertyTestClass::AgeGetter, this), 
     boost::bind(&PropertyTestClass::AgeSetter, this, _1)) 
    {} 
}; 

#endif 


/** 
* Program Entry 
**/ 
int main() { 
    std::cout << "Property Test Programm\n\n"; 

    PropertyTestClass propTest; 


    std::cout << "ReadWrite Property for age\n"; 
    propTest.age.Set(5); 
    std::cout << propTest.age.Get() << "\n"; 


    return 0; 
} 

Выход:

$ ./a.out 
Property Test Programm 

ReadWrite Property for age 
5 
+0

Большое спасибо. Теперь я понимаю, что вы имеете в виду. – Heinrich