错误:类函数 C++ 的多个定义
error: multiple definiton of class function c++
Bar.h
#ifndef BAR_H
#defind BAR_H
class Foo;
class Bar
{
Bar();
virtual ~Bar();
public:
const Foo* getP();
const Foo getS();
int getW();
void setValues (Foo* m, Foo n, int o);
protected:
Foo* p;
Foo* s;
int w;
};
#endif
福.h
#ifndef FOO_H
#define FOO_H
class Bar;
class Foo
{
Foo(int key, std::string str);
virtual ~Foo();
int id;
std::string inst;
public:
std::vector<Bar*> getBars();
std::vector<Foo*> getP();
std::vector<Foo*> getS();
protected:
std::vector<Foo*> p;
std::vector<Foo*> s;
std::vector<Bar*> e;
};
#endif
酒吧.cpp
#include <string>
#include <vector>
#include "Bar.h"
Bar::Bar()
{
w=0;
}
Bar:~Bar() {}
void Bar::setValues(Foo* m, Foo* n, int o)
{
p=m;
s=n;
w=o;
}
const Foo * Bar::getP()
{
return p;
}
const Foo* Bar::getS()
{
returns;
}
int Bar::getW()
{
return w;
}
福.cpp
#include <string>
#include <vector>
#include "Foo.h"
Foo::foo(int key, std::string str)
{
id = key;
inst = str;
}
Foo::~Foo() {}
std:vector<Bar*> Foo::getBars()
{
return e;
}
std::vector<Foo*> Foo::getP()
{
return p;
}
std::vector<Foo*> Foo::getS()
{
return s;
}
我花了 2 天时间研究我能做什么。我遵循了以下内容- 标题或包含语句中没有 *.cpp- 仅标头中的声明- *.cpp中的定义- 尝试在酒吧中添加 #include"Foo.h".cpp ->没有帮助- 尝试在 Foo 中添加 #include"Bar.h".cpp -> 没有帮助- 同时尝试上述两个步骤 ->没有帮助
我现在很想提姆,真的可以帮忙。谢谢
不能有两个同名的函数,即使它们返回不同的值。尝试在其中一个类中为 getS() 指定一个不同的名称。
此外,如果这些类未被其他类继承,则无需使析构函数成为虚拟。
检查 Bar.h 中的这一行:
void setValues (Foo* m, Foo n, int o);
然后在Bar.cpp中:
void Bar::setValues(Foo* m, Foo* n, int o)
然后标头不匹配,您必须在.hpp
中将Foo n
声明为Foo*
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何定义一个纯抽象基类