C++ - 重新定义/以前定义的错误
C++ - Redefinition/Previously defined error
为什么会出现此错误?我只是在制作一个类对象。我错过了什么?
是因为空体方法和构造函数吗?我不确定这里。
我使用的教程来自这里: 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 行,您将构造函数定义为具有空主体。此定义后面有一个无关的分号,这表明可能有意在某个时候将定义转换为声明。这一点就是现在。(错误消息中的6
和3
是指示行内的位置。编译器决定在构造函数名称的开头标记错误。
由于您显然不希望此构造函数具有空主体,因此请将第 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(
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件