几个星期休息之后,我正在尝试使用David Vandevoorde和Nicolai M. Josuttis 所着的模板 - 完整指南来扩展和扩展我的模板知识,我现在想要了解的是模板的显式实例化.
我实际上并没有这样的机制问题,但我无法想象我想要或想要使用此功能的情况.如果有人能向我解释,我将不仅仅是感激.
这让我疯狂了一个半小时.我知道这是一件小事,但找不到什么是错的(事实上,这是一个下雨的星期五下午,当然,没有帮助).
我已经定义了以下类,它将保存从文件中读取的配置参数,并允许我从我的程序中访问它们:
class VAConfig {
friend std::ostream& operator<<( std::ostream& lhs, const VAConfig& rhs);
private:
VAConfig();
static std::string configFilename;
static VAConfig* pConfigInstance;
static TiXmlDocument* pXmlDoc;
std::map<std::string, std::string> valueHash;
public:
static VAConfig* getInstance();
static void setConfigFileName( std::string& filename ) { configFilename = filename; }
virtual ~VAConfig();
void readParameterSet( std::string parameterGroupName );
template<typename T> T readParameter( const std::string parameterName );
template<typename T> T convert( const std::string& value );
};
Run Code Online (Sandbox Code Playgroud)
其中方法convert()定义VAConfig.cpp为
template <typename T>
T VAConfig::convert( const std::string& value ) …Run Code Online (Sandbox Code Playgroud) 我不能在没有在main.cpp文件中显式包含TestClass.cpp文件的情况下编译这段简单的代码.我究竟做错了什么?提前致谢!
这是代码:
TestClass.h
#ifndef TESTCLASS_H_
#define TESTCLASS_H_
class TestClass
{
public:
static int foo();
};
#endif
Run Code Online (Sandbox Code Playgroud)
TestClass.cpp
#include "TestClass.h"
int TestClass::foo() { return 42; }
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <iostream>
#include "TestClass.h"
using namespace std;
int main()
{
cout << TestClass::foo() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
g++ main.cpp -o main.app
/tmp/ccCjOhpy.o: In function `main':
main.cpp:(.text+0x18e): undefined reference to `TestClass::foo()'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud) 头文件dlist.h的一部分定义为:
#ifndef __DLIST_H__
#define __DLIST_H__
#include <iostream>
class emptyList {};
template <typename T>
class Dlist {
public:
bool isEmpty() const;
private:
struct node {
node *next;
node *prev;
T *o;
};
node *first; // The pointer to the first node (NULL if none)
node *last; // The pointer to the last node (NULL if none)
};
#include "dlist.cpp"
#endif
Run Code Online (Sandbox Code Playgroud)
当我创建这样的dlist.cpp文件时:
#include "dlist.h"
template <typename T>
bool Dlist<T>::isEmpty() const
{
return !first and !last;
}
Run Code Online (Sandbox Code Playgroud)
我在第4行收到错误消息:重新定义'bool Dlist :: isEmpty()const'
如果我删除了#include …
C ++的常见做法是将.h(或.hpp)中的声明和实现分隔为.cpp。
我知道两个主要原因(也许还有其他原因):
make预编译的.o文件进行链接)class Adepends class B和class Bon时class A )...但是我没有那么多问题,通常我可以解决它。对于面向对象的编程,它看起来像这样:
QuadraticFunction.h:
class QuadraticFunc{
public:
double a,b,c;
double eval ( double x );
double solve( double y, double &x1, double &x2 );
};
Run Code Online (Sandbox Code Playgroud)
QuadraticFunction.cpp:
#include <math.h>
#include "QuadraticFunc.h"
double QuadraticFunc::eval ( double x ){ return c + x * (b + x * a ); };
double QuadraticFunc::solve( double y, double …Run Code Online (Sandbox Code Playgroud) 我正在尝试为项目做这样的事......
template <class N>
class MyClass
{
float properties[N];
};
无论如何在C++中实现这一点?
template<typename AT>
class growVector {
int size;
AT **arr;
AT* defaultVal;
public:
growVector(int size , AT* defaultVal); //Expects number of elements (5) and default value (NULL)
AT*& operator[](unsigned pos);
int length();
void reset(int pos); //Resets an element to default value
void reset(); //Resets all elements to default value
~growVector();
};
Run Code Online (Sandbox Code Playgroud)
和.cpp是
template<typename AT>
growVector<AT>::growVector(int size, AT* defaultVal) {
arr = new AT*[size];
this->size = size;
for (int i = 0 ; i < size; i++){
arr[i] = defaultVal;
}
} …Run Code Online (Sandbox Code Playgroud) c++ ×8
templates ×5
linker ×2
allocation ×1
arrays ×1
c++-faq ×1
compilation ×1
include ×1
makefile ×1