我正在研究这个 NeuralNet 类:
class NeuralNet {
public:
// Public functions
private:
vectors vect; // Point-of-access for functions in class vectors
// Private functions
};
并且我正在使用这个极其简单的 makefile:
all: nnet.exe
nnet.exe: main.o neuralnet.o vectors.o
g++ -o nnet.exe main.o vectors.o neuralnet.o
main.o: main.cpp neuralnet.h
g++ -ggdb -c main.cpp
vectors.o: vectors.cpp vectors.h
g++ -ggdb -c vectors.cpp
neuralnet.o: neuralnet.cpp neuralnet.h vectors.h
g++ -ggdb -c neuralnet.cpp
clean:
rm -f *.o nnet.exe
当运行 g++ 来构建最终的可执行文件时,我收到以下形式的许多错误:
neuralnet.o: /path/ to/neuralnet.cpp: line# : 对向量的未定义引用::fn_name(args)
例如,我定义了一个函数:
template void fill_vec(vector&, int, double);
当我调用此函数时,我传递一个用 vector
类型声明的变量作为第一个参数,链接器报告 对 void Vectors::fill_vec(std::vector >&, int, double) 的未定义引用
对 函数的所有调用NeuralNet
实现中的>类向量
是从vect
调用的。但是,neuralnet.cpp和neuralnet.h都包含“vectors.h”,所以我假设我以某种方式链接不正确。
有谁看到任何明显的东西吗?
I'm working on this NeuralNet class:
class NeuralNet {
public:
// Public functions
private:
vectors vect; // Point-of-access for functions in class vectors
// Private functions
};
And I'm using this extremely simple makefile:
all: nnet.exe
nnet.exe: main.o neuralnet.o vectors.o
g++ -o nnet.exe main.o vectors.o neuralnet.o
main.o: main.cpp neuralnet.h
g++ -ggdb -c main.cpp
vectors.o: vectors.cpp vectors.h
g++ -ggdb -c vectors.cpp
neuralnet.o: neuralnet.cpp neuralnet.h vectors.h
g++ -ggdb -c neuralnet.cpp
clean:
rm -f *.o nnet.exe
When g++ gets run to build the final executable, I get a lot of errors in the following form:
neuralnet.o: /path/to/neuralnet.cpp: line# : undefined reference to vectors::fn_name(args)
For example, I have defined a function:
template<typename T> void fill_vec(vector<T>&, int, double);
When I call this function I pass a variable declared with type vector<double>
for the first argument, and the linker reports undefined reference to void vectors::fill_vec<double>(std::vector<double, std::allocator<double> >&, int, double)
All calls to functions of class vectors
in the implementation of NeuralNet
are called from vect
. However, both neuralnet.cpp and neuralnet.h contain includes for "vectors.h", so I'm assuming that I'm somehow linking incorrectly.
Does anyone see anything obvious?
发布评论
评论(1)
除非您在vectors.h中定义了
fn_name()
,仅包含neuralnet.cpp中的标头em> 和 neuralnet.h 还不够。确保您确实可以将手指指向该函数体。您可能希望将其放在vectors.cpp中。成为模板会改变一切。您应该在头文件中定义模板方法,而不是在 .cpp 文件中。尽管您可以在源文件中定义
fill_vec
,但编译器实际上不会为T
的任何值实例化它,因为在该范围内翻译单元,它不需要任何实例化。您可以为您需要的每个T
值手动实例化它,但在标头中声明该函数的同一位置定义该函数会更容易。这样,编译器就可以在每个需要的地方提供可用的定义。您将获得多个实例化(每个使用它的翻译单元都有一个实例化),但链接器知道如何合并这些实例化。另请参阅模板方法的未定义引用错误。
Unless you've defined
fn_name()
in-line in vectors.h, merely including that header from neuralnet.cpp and neuralnet.h is not sufficient. Make sure you can actually point your finger at that function body. You probably intended for it to be in vectors.cpp.Being a template changes everything. You should define the template methods in the header file, not in the .cpp file. Although you might define
fill_vec<T>
in your source file, the compiler doesn't actually instantiate it for any values ofT
because within that translation unit, it doesn't need any instantiations. You could manually instantiate it for every value ofT
you'll need, but it's easier to just define the function in the header at the same place you declare it. That way, the compiler has the definition available every place it's needed. You'll get multiple instantiations (one for each translation unit that uses it), but the linker knows how to consolidate those.See also Undefined reference error for template method.