我的基本模板类在Base.h中:
#include <iostream>
using std::cout;
using std::endl;
#ifndef BASE_H
#define BASE_H
template<typename T>
class Base
{
public:
Base();
Base(T a0, T b0);
void display();
private:
T a,b;
T sum();
};
#endif // BASE_H
template<typename T>
Base<T>::Base():a(0),b(0){}
template<typename T>
Base<T>::Base(T a0, T b0):a(a0),b(b0){}
template<typename T>
T Base<T>::sum()
{
return a+b;
}
template<typename T>
void Base<T>::display()
{
cout<<"The sum is: "<<sum()<<endl;
}
我的Derived.h文件是:
#ifndef DERIVED_H
#define DERIVED_H
#include <Base.h>
template<typename T>
class Derived : public Base<T>
{
public:
Derived(){}
Derived(T a0, T b0);
void display1();
};
#endif // DERIVED_H
template<typename T>
Derived<T>::Derived(T a0, T b0):Base<T>(a0,b0) {}
template<T>
void Derived<T>::display1()
{
this->display();
}
我知道模板类的实现不应该在.cpp文件中,但是当我将单独的头文件放在不同的.h文件中时,为什么会出现未定义的错误?
错误显示如下(使用code :: blocks):
***include\Base.h|24|error: redefinition of 'Base<T>::Base()'|
include\Base.h|24|error: 'Base<T>::Base()' previously declared here|***
答案 0 :(得分:1)
两个问题。首先,你的// init
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://staging-exact-integration.posios.com/PosServer/rest/registration/register');
curl_setopt($ch, CURLOPT_POST, 1);
// array fields var
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'city' => 'Gent',
'company' => 'cedric resto',
'email' => 'cedric@tills.be',
'firstName' => 'Cedric',
'lastName' => 'De Weirt',
'locale' => 'Gent',
'number' => '33',
'password' => '1234',
'phone' => '0476612438',
'referer' => '13c435f9-084a-4e79-866f-8b9800e5c14f',
'street' => 'Testlaan',
'zip'=> '9000')));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
// receive server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output;
// further processing
if (strpos($server_output,'201') == true)
{
// created
echo 'Created';
}
else
{
// catch the error(s)
echo 'Error Catch';
}
警卫是错误的。你只是守着班级宣言:
#include
如果#ifndef BASE_H
#define BASE_H
template<typename T>
class Base
{
...
};
#endif // BASE_H
... definitions of Base<T> ...
为Base.h
两次,那么您只会获得一个类声明(好)但是您将获得所有成员函数的多个定义(错误)。
#include
警卫应该保护整个文件。将#include
移至第一行,将#ifndef
移至最后一行。
第二个问题,如果你在标题中但在类声明外部提供你的成员函数定义,你必须将函数标记为#endif
(这可以在声明或定义中完成,但是我更喜欢宣言)。那就是:
inline