我不知道为什么会发生这种情况,因为我认为我已经正确地声明和定义了所有内容.
我有以下程序,使用模板设计.这是一个简单的队列实现,其成员函数为"add","substract"和"print".
我已经在精细的"nodo_colaypila.h"中为队列定义了节点:
#ifndef NODO_COLAYPILA_H
#define NODO_COLAYPILA_H
#include <iostream>
template <class T> class cola;
template <class T> class nodo_colaypila
{
T elem;
nodo_colaypila<T>* sig;
friend class cola<T>;
public:
nodo_colaypila(T, nodo_colaypila<T>*);
};
Run Code Online (Sandbox Code Playgroud)
然后在"nodo_colaypila.cpp"中实现
#include "nodo_colaypila.h"
#include <iostream>
template <class T> nodo_colaypila<T>::nodo_colaypila(T a, nodo_colaypila<T>* siguiente = NULL)
{
elem = a;
sig = siguiente;//ctor
}
Run Code Online (Sandbox Code Playgroud)
然后,队列模板类的定义和声明及其功能:
"cola.h":
#ifndef COLA_H
#define COLA_H
#include "nodo_colaypila.h"
template <class T> class cola
{
nodo_colaypila<T>* ult, pri;
public:
cola<T>();
void anade(T&);
T saca();
void print() const;
virtual …Run Code Online (Sandbox Code Playgroud) 我目前正在研究一些日志代码,它们应该 - 除其他外 - 打印有关调用函数的信息.这应该相对容易,标准C++有一个type_info类.它包含typeid'd类/函数/ etc的名称.但它被破坏了.它不是很有用.即typeid(std::vector<int>).name()回归St6vectorIiSaIiEE.
有没有办法从中产生有用的东西?就像std::vector<int>上面的例子一样.如果它只适用于非模板类,那也没关系.
该解决方案应该适用于gcc,但如果我可以移植它会更好.这是为了记录所以它不是那么重要,它不能被关闭,但它应该有助于调试.
当我在我的main函数中调用模板类"add"和"greater"中的两个函数时,我一直得到未定义的引用.
所以,我有:number.h
#ifndef NUMBER_H
#define NUMBER_H
template <class T>
class number {
public:
T x;
T y;
number (int a, int b){
x=a; y=b;}
int add (T&);
T greater ();
};
#endif
Run Code Online (Sandbox Code Playgroud)
number.cpp
#include "number.h"
template <class T>
int number<T>::add (T& rezAdd){
rezAdd = x+y;
return 1;
}
template <class T>
T number<T>::greater (){
return x>y? x : y;
}
Run Code Online (Sandbox Code Playgroud)
我的主文件是:resolver.cpp
#include <stdio.h>
#include <stdlib.h>
#include "number.h"
int main (int argc, char **argv) {
int aux;
number<int> c(3,5);
c.add(aux);
printf …Run Code Online (Sandbox Code Playgroud) 我尝试查找这个并使用头文件等获得混合结果.
基本上我有多个cpp文件,其中包含我用于二叉树,BST,链表等的所有功能.
我想要做的不是必须复制和粘贴函数,因为我需要它们,我希望能够做到
#include <myBSTFunctions.h>
Run Code Online (Sandbox Code Playgroud)
并能够调用和使用我自己的功能.
完成此任务的步骤是什么?使用我使用的所有函数原型制作头文件?在哪里使用所有实际功能来放置cpp和头文件?有没有办法可以直接调用函数文件的目录?
即我更想把它与主源cpp文件放在同一个文件夹中,与我的一些同事分享.
我怎么能做到这一点?
编辑:Windows.miniGW编译器