相关疑难解决方法(0)

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

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

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

为什么是这样?

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

c++ templates c++-faq

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

C++和Java中"泛型"类型之间有什么区别?

Java有泛型,C++提供了一个非常强大的编程模型template.那么,C++和Java泛型有什么区别?

c++ java generics language-features templates

150
推荐指数
6
解决办法
9万
查看次数

将export关键字与模板一起使用

正如我所理解的那样,可以使用"export"关键字,以便可以通过头文件公开模板类或函数签名,并在库文件中抽象实际实现.
任何人都可以提供一个实用的示例程序,说明如何做到这一点?
使用此功能时是否有任何缺点或重点?

编辑:基于答案的后续问题.正如答案中所提到的,'export'在C++ 0x中已被弃用,即使对于C++ 03x也很少得到编译器的支持.鉴于这种情况,可以用什么方式隐藏lib文件中的实际实现,只是通过头文件公开声明,以便最终用户可以知道暴露的API的签名是什么,但是无法访问实现相同的源代码?

c++ templates export c++11

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

如何创建一个对模板类执行操作的静态模板成员函数?

我正在尝试创建一个通用函数,从std :: vector中删除重复项.由于我不想为每个矢量类型创建一个函数,我想让它成为一个可以接受任何类型矢量的模板函数.这是我有的:

//foo.h

Class Foo {

template<typename T>
static void RemoveVectorDuplicates(std::vector<T>& vectorToUpdate);

};

//foo.cpp

template<typename T>
void Foo::RemoveVectorDuplicates(std::vector<T>& vectorToUpdate) {
for(typename T::iterator sourceIter = vectorToUpdate.begin(); (sourceIter != vectorToUpdate.end() - 1); sourceIter++) {
        for(typename T::iterator compareIter = (vectorToUpdate.begin() + 1); compareIter != vectorToUpdate.end(); compareIter++) {
            if(sourceIter == compareIter) {
                vectorToUpdate.erase(compareIter);
            }
        }
    }
}

//SomeOtherClass.cpp

#include "foo.h"

...

void SomeOtherClass::SomeFunction(void) {
    std::vector<int> myVector;

    //fill vector with values

    Foo::RemoveVectorDuplicates(myVector);
}
Run Code Online (Sandbox Code Playgroud)

我一直收到链接器错误,但它编译得很好.关于我做错了什么的任何想法?

更新:基于Iraimbilanja给出的答案,我去重写了代码.但是,万一有人想要使用代码来执行RemoveDuplicates函数,这里是:

//foo.h

Class Foo {

    template<typename T>
    static void RemoveVectorDuplicates(T& …
Run Code Online (Sandbox Code Playgroud)

c++ templates stl vector static-members

25
推荐指数
2
解决办法
5万
查看次数

如何强制使用模板专业化?

我试图让我的模板函数产生编译时错误,如果实例化非专用基本版本.我尝试了通常的编译时断言模式(负数组大小),但即使没有实例化模板,编译也会失败.当且仅当基础模板函数被实例化时,有关如何使其失败的任何想法?

template<class Foo> void func(Foo x) {
  // I want the compiler to complain only if this function is instantiated.
  // Instead, the compiler is complaining here at the declaration.
  int Must_Use_Specialization[-1];
}

template<> void func(int x) {
  printf("Hi\n");
}
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization

13
推荐指数
4
解决办法
3463
查看次数

在cpp中定义模板专业化?

我可以在cpp中定义一个专门的函数,就像这样......

//标题

template<typename T>
void func(T){}

template<>
void func<int>(int);
Run Code Online (Sandbox Code Playgroud)

// cpp

template<>
void func<int>(int)
{}
Run Code Online (Sandbox Code Playgroud)

如何在cpp中的专用类中定义方法?像这样(这不起作用,我得到error C2910: 'A<int>::func' : cannot be explicitly specialized)......

//标题

template<typename T>
struct A
{
    static void func(T){}
};

template<>
struct A<int>
{
    static void func(int);
};
Run Code Online (Sandbox Code Playgroud)

// cpp

template<>
void A<int>::func(int)
{}
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization visual-studio

12
推荐指数
1
解决办法
1万
查看次数

我什么时候应该使用-inl.h文件?

我刚刚在Google C++编码风格指南中注意到了这个项目 - 我并不太明白.

如果我将内联方法或函数放在除其他文件包含的标题之外的文件中,它将不是该类的方法; 它只能用于包含它的代码.那么为什么甚至会有这样的-inl.h文件呢?

另外,为什么我们甚至想要内联长函数呢?(即,除了模板的情况,我们必须将代码放在头文件中进行实例化)

c++ inline header header-files

9
推荐指数
3
解决办法
4174
查看次数

在模板类实现中分离.h和.cpp文件

我正在尝试编写一个模板类,可以根据<class>我的传递形成类.问题是我不能在同一个.h文件中声明和定义.在我的项目中,UTF工具只能处理.cpp文件(代码覆盖等).我在博客中看到他们说"添加.cpp而不是.h".这是可取的吗?

Template.h

#ifndef TEMPLATE_H_
#define TEMPLATE_H_

template<class T>
class Template
{
public:
    T Add(T a,T b);
};

#endif /* TEMPLATE_H_ */
Run Code Online (Sandbox Code Playgroud)

Template.cpp

#include "Template.h"

template<class T>
T Template<T>::Add(T a, T b)
{
    return a+b;
}
Run Code Online (Sandbox Code Playgroud)

Main.cpp

#include "Template.cpp" //Is this a good practise? 
#include <iostream>

int main(int argc, char **argv) {
    Template<int> obj;
    std::cout<<obj.Add(3,4)<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

如果这不可行,那么我该如何解决这个问题呢?export

c++ templates

8
推荐指数
2
解决办法
2万
查看次数

在 C++ 中重新定义 template&lt;class T&gt;

我已经搜索并搜索了我的问题的解决方案,但我似乎找不到。我正在使用 Code::Blocks 并且我收到模板类的重新定义错误。

这是我的“vectorAux.h”文件:

#ifndef vectoraux_h
#define vectoraux_h

#include <vector>
#include <algorithm>
#include <iostream>

template <typename T>
void removeDup(std::vector<T> & v);

template <typename T>
unsigned seqVectSearch(const std::vector<T> & v, unsigned first,
               unsigned last, const T& target);

template <typename T>
void writeVector(const std::vector<T> & v);

#include "vectorAux.cpp"
#endif
Run Code Online (Sandbox Code Playgroud)

这是我的“vectorAux.cpp”文件:

#include "vectorAux.h"

#include <vector>
#include <algorithm>
#include <iostream>

template <typename T>
void removeDup(std::vector<T> & v)
{
    std::vector<int> vector1;
    unsigned i, last = v.size();

    for(int j = 0; j <= v.size(); j++)
    { …
Run Code Online (Sandbox Code Playgroud)

c++ templates header redefinition

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

错误:在'<'标记之前的预期构造函数,析构函数或类型转换

我是C++的新手,但已经在java中获得了一些经验.

在下面的简短c ++练习中,我尝试使用类模板构建堆栈.不幸的是它无法编译,我无法弄清楚原因.

错误消息是:

Stack.cpp:6:错误:预期构造,析构函数,或类型之前转换‘<’令牌
Stack.cpp:14:错误:前预期初始‘<’令牌
Stack.cpp:25:错误:预期初始化之前‘<’令牌
使[2]:* [构建/Debug/GNU-Linux-x86/Stack.o]错误1

这是Stack.h:

template <class T>
class Stack {
public:
    Stack(int = 10);

    ~Stack() {
        delete [] stackPtr;
    }

    int isEmpty()const {
        return top == -1;
    }

    int isFull() const {
        return top == size - 1;
    }

    int push(const T&);
    int pop(T&);

private:
    int size; // length i.e. number of elements on Stack.
    int top; //index of top element
    T* stackPtr;
};
Run Code Online (Sandbox Code Playgroud)

Stack.cpp:

template <class …
Run Code Online (Sandbox Code Playgroud)

c++ templates

5
推荐指数
1
解决办法
1737
查看次数