我在程序中使用虚拟功能时收到 LNK2005、LNK2001 和 LNK1120

发布于 2025-01-11 00:33:10 字数 1082 浏览 0 评论 0原文

我是一个初学者,所以这个问题对你来说可能看起来微不足道。 所以我有以下文件:

  • base.h
  • 衍生
  • .hbase.cpp
  • 衍生
  • .cppTestCpp.cpp

base.h

#include <iostream>

namespace App
{
    class Base
    {
    public:
        virtual void Print();
    };
}

base.cpp

#include "base.h"

namespace App
{
}

衍生.h

#include "base.h"

class Derived : public App::Base
{
public:
    void Print();
};

衍生.cpp

#include "derived.h"

void Derived::Print()
{
    std::cout << "This works!! form Derived class\n";
}

最后 TestCpp.cpp

#include "derived.h"

int main()
{
    std::cout << "Hello World!\n";
    Derived d;
    d.Print();
    return 0;
}

我收到以下链接器错误: 输入图片这里的描述

我不知道我做错了什么。请帮帮我。

I am a beginner so this problem might seem trivial to you.
So I have the following files:

  • base.h
  • derived.h
  • base.cpp
  • derived.cpp
  • TestCpp.cpp

base.h

#include <iostream>

namespace App
{
    class Base
    {
    public:
        virtual void Print();
    };
}

base.cpp

#include "base.h"

namespace App
{
}

derived.h

#include "base.h"

class Derived : public App::Base
{
public:
    void Print();
};

derived.cpp

#include "derived.h"

void Derived::Print()
{
    std::cout << "This works!! form Derived class\n";
}

and at last
TestCpp.cpp

#include "derived.h"

int main()
{
    std::cout << "Hello World!\n";
    Derived d;
    d.Print();
    return 0;
}

I am getting the following Linker error:
enter image description here

I don't know what it is I am doing wrong. Please help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2025-01-18 00:33:10

问题是您仅在Base类中声明虚函数Print,但没有定义它。

以及C++03 标准:10.3 虚函数 [class.virtual]

在类中声明的虚函数应在该类中定义或声明为纯函数(10.4),或两者兼而有之;但不需要诊断(3.2)。

因此解决这个问题的方法是在类Base中实现/定义virtual成员函数Print或者通过将类 Base 中的声明替换为 virtual void Print() = 0; 使其成为纯虚拟

解决方案 1

Base.cpp

#include "base.h"

namespace App
{
    void Base::Print()
    {
        
    }
}

解决方案 1 DEMO

解决方案 2

Base.h

#include <iostream>

namespace App
{
    class Base
    {
    public:
        virtual void Print() = 0;
    };
}

解决方案 2 演示

The problem is that you've only declared the virtual function Print in class Base but not defined it.

And from C++03 Standard: 10.3 Virtual functions [class.virtual]

A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).

So the way to solve this problem would be to either implement/define the virtual member function Print in class Base or make it pure virtual by replacing the declaration with virtual void Print() = 0; inside class Base

Solution 1

Base.cpp

#include "base.h"

namespace App
{
    void Base::Print()
    {
        
    }
}

Solution 1 DEMO

Solution 2

Base.h

#include <iostream>

namespace App
{
    class Base
    {
    public:
        virtual void Print() = 0;
    };
}

Solution 2 DEMO

人心善变 2025-01-18 00:33:10

只需在 base.cpp 文件中添加 Print() 的定义即可:

namespace App
{
    void Base::Print() { }
}

它里面可能没有任何内容,但它应该在那里。

或者通过使其纯粹来指示 Print() 在派生类中定义:这

virtual void Print() = 0;

2 个选项中的任何一个都有效。另外,在派生类中,最好将 Print() 设为 override:(单击此处了解原因

class Derived : public App::Base
{
public:
    void Print() override;
};

Just add a definition for Print() in the base.cpp file:

namespace App
{
    void Base::Print() { }
}

It may not have anything inside it, but it should be there.

Or indicate Print() to be defined in the derived class by making it pure:

virtual void Print() = 0;

Any one of the 2 options work. Also in your derived class it's better to make Print() as override: (Click here to know why)

class Derived : public App::Base
{
public:
    void Print() override;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文