C++ - 重新定义/以前定义的错误

C++ - Redefinition/Previously defined error

本文关键字:定义 错误 C++ 新定义      更新时间:2023-10-16

为什么会出现此错误?我只是在制作一个类对象。我错过了什么?

是因为空体方法和构造函数吗?我不确定这里。

我使用的教程来自这里: https://www.youtube.com/watch?v=KkwX7FkLfug

法典:

#include <vector>
#include <iostream>
class Neuron {};
typedef std::vector<Neuron> Layer;
class Net
{
public:
Net(const std::vector<unsigned> &topology) {};
void feedForward(const std::vector<double> &inputVals) {};
void backProp(const std::vector<double> &targetVals) {};
void getResults(std::vector<double> &resultVals) const {};
private:
// [layerNum][neuronNum]
std::vector<Layer> m_layers;
};
void Net::Net(const std::vector<unsigned> &topology)
{
unsigned numLayers = topology.size();
for (unsigned layerNum = 0; layerNum < numLayers; ++layerNum){
m_layers.push_back(Layer());
for (unsigned neuronNum = 0; neuronNum <= topology[layerNum]; ++neuronNum){
m_layers.back().push_back(Neuron());
std::cout << "Made a neuron!" << std::endl;
}
}
}
int main(int argc, char *argv[])
{
std::vector<unsigned> topology;
topology.push_back(3);
topology.push_back(2);
topology.push_back(1);
Net myNet(topology);
std::vector<double> inputVals;
myNet.feedForward(inputVals);
std::vector<double> targetVals;
myNet.backProp(targetVals);
std::vector<double> resultVals;
myNet.getResults(resultVals);
return 0;
}

运行:

g++ e:/something/ProgrammingExt/0a_Testing/cpp/neural_network/neural-net-tutorial.cpp

收到此错误:

ERROR (0.39 seconds): e:/something/ProgrammingExt/0a_Testing/cpp/neural_network/neural-net-
tutorial.cpp:20:52: error: return type specification for constructor invalid
void Net::Net(const std::vector<unsigned> &topology)
^
e:/something/ProgrammingExt/0a_Testing/cpp/neural_network/neural-net-tutorial.cpp:20:6: error: redefinition of ’Net::Net(const std::vector<unsigned int>&)’
void Net::Net(const std::vector<unsigned> &topology)
^~~
e:/something/0a_Testing/cpp/neural_network/neural-net-tutorial.cpp:11:3: note: ’Net::Net(const std::vector<unsigned int>&)’ previously defined here
Net(const std::vector<unsigned> &topology) {};
^~~

错误消息告诉您为什么会收到它们。

ERROR (0.39 seconds): [path]/neural-net-tutorial.cpp:20:52: error: return type specification for constructor invalid
void Net::Net(const std::vector<unsigned> &topology)
^

这是第一个错误。构造函数不返回任何内容,甚至不返回void。因此,从指示的行中删除void关键字。

[path]/neural-net-tutorial.cpp:20:6: error: redefinition of ’Net::Net(const std::vector<unsigned int>&)’
void Net::Net(const std::vector<unsigned> &topology)
^~~
[path]/neural-net-tutorial.cpp:11:3: note: ’Net::Net(const std::vector<unsigned int>&)’ previously defined here
Net(const std::vector<unsigned> &topology) {};
^~~

这是第二个错误。对于采用const std::vector<unsigned> &参数的Net构造函数,有两个构造函数的定义。副本位于第 20 行,与触发有关void的早期错误的同一行。原始版本位于第 11 行,您将构造函数定义为具有空主体。此定义后面有一个无关的分号,这表明可能有意在某个时候将定义转换为声明。这一点就是现在。(错误消息中的63是指示行内的位置。编译器决定在构造函数名称的开头标记错误。

由于您显然不希望此构造函数具有空主体,因此请将第 11 行中的定义从

Net(const std::vector<unsigned> &topology) {};

通过删除函数体来声明:

Net(const std::vector<unsigned> &topology);

C++重载不能基于返回类型。因此,有两个函数具有完全相同的参数,并且它们被视为相同的函数:

Net(const std::vector<unsigned> &topology) {};

void Net::Net(const std::vector<unsigned> &topology)
{
unsigned numLayers = topology.size();
for (unsigned layerNum = 0; layerNum < numLayers; ++layerNum){
m_layers.push_back(Layer());
for (unsigned neuronNum = 0; neuronNum <= topology[layerNum]; ++neuronNum){
m_layers.back().push_back(Neuron());
std::cout << "Made a neuron!" << std::endl;
}
}
}

这就是您遇到重定义错误的原因。

另一个问题是构造函数没有返回类型,但你为第二个定义提供了一个"void"。这就是您收到第一个错误的原因: 教程.cpp:20:52:错误:返回构造函数的类型规范无效 void Net::Net(const std::vector &topology(