C++ 使用根项目目录以外的源文件(搜索路径中包含的文件)时出现链接错误(Visual C+;+;)

C++ 使用根项目目录以外的源文件(搜索路径中包含的文件)时出现链接错误(Visual C+;+;),c++,visual-studio,linker,extern,C++,Visual Studio,Linker,Extern,我一直在编写一个模板结构,在它工作之后,我决定在其他项目上使用它。该模板包含两个文件。ListOctree.cpp和ListOctree.h。 虽然它们自己编译并运行得很好(我可以运行结构自检) 在另一个项目上使用它们时,ListOctree.cpp/.h都在./Util目录下,但尽管Visual Studio似乎找到了.h文件(我可以在项目的任何地方使用.h声明),但似乎找不到.cpp源文件,其中声明了.h上声明的所有代码 这些文件是项目的一部分。这棵树是这样的: program.cpp &l

我一直在编写一个模板结构,在它工作之后,我决定在其他项目上使用它。该模板包含两个文件。ListOctree.cpp和ListOctree.h。 虽然它们自己编译并运行得很好(我可以运行结构自检)

在另一个项目上使用它们时,ListOctree.cpp/.h都在./Util目录下,但尽管Visual Studio似乎找到了.h文件(我可以在项目的任何地方使用.h声明),但似乎找不到.cpp源文件,其中声明了.h上声明的所有代码


这些文件是项目的一部分。这棵树是这样的:

program.cpp <-- includes "Util/ListOctree.h"
/Util
    ListOctree.cpp
    ListOctree.h
program.cpp擦除(项);};
///清除节点内容
void clearNode(){c->clear();};
};
/**
*@see节点类的@short容器
*
*这个类允许更方便地使用八叉树
*/
模板类ListOctree{
//属性
私人:
整数宽度;
节点*根;
//方法
公众:
列表八叉树(整数宽度);
~ListOctree();
无效pushItemAt(T项,int x,int y,int z);
无效项(T项,整数x,整数y,整数z);
无效clearItemsAt(整数x,整数y,整数z);
std::list*getItems();
std::list*操作符()(intx,inty,intz){return(getItemsAt(x,y,z));};
std::list*getItemsAt(intx,inty,intz);
私人:
char nextNode(int x,int y,int z,int*cx,int*cy,int*cz,int width,Node*n,bool createNew)抛出(…);
void _pushItemAt(T项,int x,int y,int z,int cx,int cy,int cz,int width,Node*n);
void _eraseItemAt(T项,int x,int y,int z,int cx,int cy,int cz,int width,Node*n);
void _clearItemsAt(int x,int y,int z,int cx,int cy,int cz,int width,Node*n);
std::list*_getItemsAt(int x,int y,int z,int cx,int cy,int cz,int width,Node*n);
空洞修剪(节点*n);
void addItemsToList(std::list*lst,Node*n);
};
/**
*@内部使用的简短异常
* 
*FIXME,从未使用过,由于某种原因无法使用
*/
类ListOctreeException:public std::exception{
私人:
std::字符串msg;
公众:
ListOctreeException(){msg=“ListOctreeException”;};
ListOctreeException(std::string s){msg=s;};
const char*what(){return(msg.c_str());};
};
};//名称空间
#恩迪夫

您是否在解决方案中的项目之间添加了引用?(Visual Studio 2010)

右键单击项目->参考


或者,您是否添加了项目依赖项?(Visual Studio 2008)

您是否在解决方案中的项目之间添加了引用?(Visual Studio 2010)

右键单击项目->参考


或者,您是否添加了项目依赖项?(Visual Studio 2008)

模板的声明和定义应保存在同一个文件中(通常为.h)。有关详细信息,请参阅(从技术上讲,也可以在包含实现()的文件中实例化模板)


关键字
export
,本应在两者分离的情况下使用,但很少有编译器实现它,现在它已在c++0x中删除

模板的声明和定义应保存在同一个文件中(通常是.h)。有关详细信息,请参阅(从技术上讲,也可以在包含实现()的文件中实例化模板)



关键字
export
,本应在两者分离的情况下使用,但很少有编译器实现它,现在它已在c++0x中删除

发布标题和源。发布标题和源。文件是项目的一部分。一个项目一个解决方案。没有尝试使用多个项目那么你应该在你的问题中澄清这一点。您声明,“在其他项目上使用它们时…”。但由于这不是问题所在,我们将无法在没有看到您的一些源代码的情况下帮助您。添加了.h文件以及我如何在.cpp上使用它。但奇怪的是,编译器可以编译(冗余;),但生成的代码无法链接,即使它们是同一项目的一部分(编辑,我知道列表->擦除(项)是错误的,但没有使用该方法。我需要将其从节点类中删除)文件是项目的一部分。一个项目一个解决方案。没有尝试使用多个项目那么你应该在你的问题中澄清这一点。您声明,“在其他项目上使用它们时…”。但由于这不是问题所在,我们将无法在没有看到您的一些源代码的情况下帮助您。添加了.h文件以及我如何在.cpp上使用它。仍然奇怪的是,编译器可以编译(冗余;),但生成的代码不能链接,即使它们是同一项目的一部分(编辑,我知道list->erase(item)是错误的,但没有使用该方法。我需要将其从节点类中删除),所以所有模板代码都必须插入到.h文件中?.cpp相当大。也。可以这样做:cat ListOctree.h ListOctree.cpp>ListOctree\u new.h,然后包括新文件。@NeonMan是的,然后可以包括.h。@NeonMan是的,那也可以,但是你不会在项目中包括
ListOctree.cpp
。@jonsca谢谢!在发布后就尝试过了,它确实有效。现在该程序包括ListOctree_fat.h,其中包括原始的.h和.cpp(还不能启动),所以所有模板代码都必须插入到.h文件中?.cpp相当大。也。可以这样做:cat ListOctree.h ListOctree.cpp>ListOctree\u new.h,然后包括新文件。@NeonMan是的,然后可以包括.h。@NeonMan是的,那也可以,但是你不会在项目中包括
ListOctree.cpp
。@jonsca谢谢!在发布后就尝试过了,它确实有效。现在,该程序包括ListOctree_fat.h,其中包括原始的.h和.cpp(不能使用)
#include "Util/ListOctree.h"
//...
void main (){
//...
    ListOctree<int>* a = new ListOctree<int>(8);
//...
}
#ifndef __UTIL_LISTOCTREE
#define __UTIL_LISTOCTREE

//Conditional compilation flags

#undef _SELFTEST
#undef _DEBUG_LISTOCTREE

//Conditional compilation options
#ifdef _DEBUG_LISTOCTREE
 #undef  _DEALLOCATE_LISTS
 #define _SELFTEST
#endif

#ifdef _SELFTEST
 #include <iostream>
#endif

#ifdef _SELFTEST
 #include<assert.h>
#endif

#ifdef _DEBUG_LISTOCTREE
 #include <iostream>
#endif
/////////////////////////////////

#include <deque>
#include <list>
#include <iterator>
#include <math.h>
#include <exception>

//Remind me to never do this again. (xYz,XYz,XYZ,xYZ,xyz,Xyz,XyZ,xyZ,)
//A CAPS character means positive in the axis while a
//Lowercase character means negative
//FIXME Never used I think.
#define _xyz sons[0]
#define _xyZ sons[1]
#define _xYz sons[2]
#define _xYZ sons[3]
#define _Xyz sons[4]
#define _XyZ sons[5]
#define _XYz sons[6]
#define _XYZ sons[7]

namespace Util{
/**
 * @brief The node class
 *
 * Each node stores up to 8 sons, a parent pointer and, if it
 * is a Leaf node, a list of contents.
 */
template <typename T> class Node{
    //Attributes
public:
    ///An array with all the sons
    Node<T>* sons[8];
    ///A parent pointer
    Node<T>* parent;

    //The node contents (if any)
    std::list<T>* c;

    //Methods
public:
    /// Deallocates itself AND all of it's sons
    ~Node();
    /// Creates an empty node
    Node();
    /// Creates a node with its eight sons and a parent reference from an 9 pointer array
    Node(Node<T>** nodes);
    ///Returns the node contents
    std::list<T>* getContents(){return c;};
    ///Pushes an item into the node
    void pushItem(T item);
    ///Removes an item from the node
    void eraseItem(T item){c->erase(item);};
    ///Clears the node contents
    void clearNode(){c->clear();};
};

/**
 * @brief Container for @see Node classes
 *
 * This class allows a more convenient way of working with Octrees
 */
template <typename T> class ListOctree {
    //Attributes
private:
    int width;
    Node<T>* root;

    //Methods
public:
    ListOctree(int width);
    ~ListOctree();
    void pushItemAt(T item, int x, int y, int z);
    void eraseItemAt(T item, int x, int y, int z);
    void clearItemsAt(int x, int y, int z);
    std::list<T>* getItems();
    std::list<T>* operator() (int x, int y, int z){return(getItemsAt(x,y,z));};
    std::list<T>* getItemsAt(int x, int y, int z);
private:
    char nextNode(int x, int y, int z, int* cx, int* cy, int* cz, int width, Node<T>* n, bool createNew) throw(...);
    void _pushItemAt(T item, int x, int y, int z, int cx, int cy, int cz, int width, Node<T>* n);
    void _eraseItemAt(T item, int x, int y, int z, int cx, int cy, int cz, int width, Node<T>* n);
    void _clearItemsAt(int x, int y, int z, int cx, int cy, int cz, int width, Node<T>* n);
    std::list<T>* _getItemsAt(int x, int y, int z, int cx, int cy, int cz, int width, Node<T>* n);
    void prune(Node<T>* n);
    void addItemsToList(std::list<T>* lst, Node<T>* n);
};

/**
 * @brief Exception used internally
 * 
 * FIXME, never used, can't be used for some reason
 */
class ListOctreeException : public std::exception{
private:
    std::string msg;

public:
    ListOctreeException(){msg="ListOctreeException";};
    ListOctreeException(std::string s){msg=s;};
    const char* what(){return(msg.c_str());};
};
};//namespace
#endif