相关疑难解决方法(0)

为什么模板只能在头文件中实现?

引自C++标准库:教程和手册:

目前使用模板的唯一可移植方法是使用内联函数在头文件中实现它们.

为什么是这样?

(澄清:头文件不是唯一的便携式解决方案.但它们是最方便的便携式解决方案.)

c++ templates c++-faq

1660
推荐指数
14
解决办法
46万
查看次数

显式实例化 - 何时使用?

几个星期休息之后,我正在尝试使用David Vandevoorde和Nicolai M. Josuttis 所着的模板 - 完整指南来扩展和扩展我的模板知识,我现在想要了解的是模板的显式实例化.

我实际上并没有这样的机制问题,但我无法想象我想要或想要使用此功能的情况.如果有人能向我解释,我将不仅仅是感激.

c++ templates

73
推荐指数
3
解决办法
5万
查看次数

模板方法的未定义引用错误

这让我疯狂了一个半小时.我知道这是一件小事,但找不到什么是错的(事实上,这是一个下雨的星期五下午,当然,没有帮助).

我已经定义了以下类,它将保存从文件中读取的配置参数,并允许我从我的程序中访问它们:

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)

c++ linker templates compilation

43
推荐指数
3
解决办法
3万
查看次数

C++未定义参考(即使包含)

我不能在没有在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)

c++ include

8
推荐指数
1
解决办法
9160
查看次数

从.h中定义.cpp中的模板化函数(获取错误)

头文件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++

3
推荐指数
1
解决办法
229
查看次数

(避免)在C ++中将代码拆分为.cpp和.h并进行高效编译

C ++的常见做法是将.h(或.hpp)中的声明和实现分隔为.cpp

我知道两个主要原因(也许还有其他原因):

  1. 编译速度(仅更改一个文件就不必重新编译所有文件,可以通过make预编译的.o文件进行链接)
  2. 前向声明有时是必需的(当执行class Adepends class Bclass 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)

c++ makefile code-organization code-maintainability

2
推荐指数
1
解决办法
945
查看次数

C++模板化,静态分配的数组

我正在尝试为项目做这样的事......

template <class N> class MyClass { float properties[N]; };

无论如何在C++中实现这一点?

c++ arrays templates allocation

0
推荐指数
1
解决办法
186
查看次数

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++ linker templates

-3
推荐指数
1
解决办法
494
查看次数