错误:类函数 C++ 的多个定义

error: multiple definiton of class function c++

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

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*