我有一个带有模板化函数的基类,它具有一般模板类型,以及专用版本.
#ifndef BASE_CLASS
#define BASE_CLASS
#include <iostream>
using namespace std;
struct Type1
{
};
struct Type2
{
};
class baseClass
{
public:
template<class Type>
void doStuff(Type & t)
{
templateFunction(t);
}
template<class Type>
void templateFunction(Type & t);
};
template<class Type>
void baseClass::templateFunction(Type & t)
{
cout << "This is the generic function!" << endl;
}
template<>
void baseClass::templateFunction(Type1 & t)
{
cout << "This is the specialized function: - Type1" << endl;
}
#endif
Run Code Online (Sandbox Code Playgroud)
我还有一个继承自"baseClass"的子类.但是,子类需要不同的专业化功能.
#ifndef CHILD_CLASS
#define CHILD_CLASS
#include …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
struct test
{
int i;
double h;
int j;
};
int main()
{
test te;
te.i = 5;
te.h = 6.5;
te.j = 10;
cout << "size of an int: " << sizeof(int) << endl; // Should be 4
cout << "size of a double: " << sizeof(double) << endl; //Should be 8
cout << "size of test: " << sizeof(test) << endl; // Should be 24 (word size of 8 for double)
//These two …Run Code Online (Sandbox Code Playgroud) 不要问为什么,但有没有办法抑制失败的链接错误?
如:
未定义的BLANK参考
这是在GCC 3.4.6中
所以...当我去的时候:
cout<<stringName<<endl;
Run Code Online (Sandbox Code Playgroud)
我明白了:
NT
Run Code Online (Sandbox Code Playgroud)
但是当我走的时候:
cout<<stringName.c_str()<<endl;
Run Code Online (Sandbox Code Playgroud)
我明白了:
NTNT
Run Code Online (Sandbox Code Playgroud)
为什么?