【问题标题】:Trouble with Polygon in Stroustrup's PPP bookStroustrup 的 PPP 书中的多边形问题
【发布时间】:2014-01-25 05:47:25
【问题描述】:

我阅读了 Stroustrup 的书 Programming Principles and Practice using C++。在第 12 章和第 441 页有这段代码:

//
// This is example code from Chapter 12.3 "A first example" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#include "Simple_window.h"    // get access to our window library
#include "Graph.h"            // get access to our graphics library facilities

//------------------------------------------------------------------------------

int main()
{
    using namespace Graph_lib;   // our graphics facilities are in Graph_lib

    Point tl(100,100);           // to become top left  corner of window

    Simple_window win(tl,600,400,"Canvas");    // make a simple window

    Polygon poly;                // make a shape (a polygon)

    poly.add(Point(300,200));    // add a point
    poly.add(Point(350,100));    // add another point
    poly.add(Point(400,200));    // add a third point 

    poly.set_color(Color::red);  // adjust properties of poly

    win.attach (poly);           // connect poly to the window

    win.wait_for_button();       // give control to the display engine
}

//------------------------------------------------------------------------------

当我运行代码时,我得到了 13 个错误,其中肯定是关于 Polygon 标识符的。例如第一个错误是: 错误 C2872:“多边形”:不明确的符号

为什么我的编译器不知道Polygon

【问题讨论】:

  • 多边形在哪里定义?我没有这本书。
  • 您有标头 (.h) 文件吗?
  • 可能,在全局命名空间和namespace Graph_lib 中都有一个同名的类型,而 using 指令会导致歧义。如果是这种情况,请使用限定名称 ::PolygonGraph_lib::Polygon,具体取决于您的需要。
  • @Peter:我的 include 目录中有代码中提到的所有头文件(“Simple_window.h”和“Graph.h”)。
  • @abbasi:这意味着要么你没有实现Graph_lib::Window::draw(),要么你没有链接到包含实现的文件或库。

标签: c++ user-interface


【解决方案1】:

如果符号不明确,则尝试使用其限定名称:

Graph_lib::Polygon poly;

【讨论】:

  • 我使用了这个片段代码 Graph_lib::Polygon poly; 而不是这个 Polygon poly; 并再次运行代码。再次出现 11 个错误,首先是:Error 9 error LNK2001: unresolved external symbol "protected: virtual void __thiscall Graph_lib::Window::draw(void)" (?draw@Window@Graph_lib@@MAEXXZ) C:\Users \CS\documents\visual studio 2012\Projects\Win32Project1\Win32Project1\Win32Project1.obj
  • 似乎链接器没有看到有函数定义的库 Graph。您必须确保在项目中正确指定了库路径。
  • 每个人对头文件所做的就是将它们添加到 include 目录中,然后编译器(此处为 Visual Studio)读取它们。这正是我所做的,即我已将 Graph.h 标头添加到 include 目录中。而且我不知道为编译器提供头文件路径的另一种方法。我现在想到的是创建 Graph.h 代码的头文件,并将其替换为 include 中以前的 graph.h 头文件 目录。同意吗?
  • 我认为除了标题之外,还有一些包含定义的库文件。你应该告诉链接器在哪里搜索它们。
  • 我不知道该怎么做。这是一个非常全球性的答案。无论如何,感谢您的回复。
【解决方案2】:

环境:操作系统:Win 10 Pro 1909,VS 2019 v16.4.5

我正在阅读 Stroupstrup 的 PPP 书的第二版,并且还收到错误消息“多边形不明确”。

我认为问题与路径有关。当我打开 x64 Native Tools Command Prompt for VS 2019 并输入 Path 时,我找到了对 ..\Program Files (x86)\Windows Kits 的引用\10\Windows 性能工具包\。当我在 Visual Studio 中执行 Find in Files 并在 Visual C++ 包含目录 中搜索单词 Polygon 时,我得到了 35 个结果。结果之一是 Windows.UI.Xaml.Shapes.0.h 头文件中名为 Polygon 的结构化变量。此头文件与上述 Windows Kits 目录位于同一目录中。

假设 Visual Studio 加载到与 x64 Native Tools Command Prompt for VS 2019 相同的路径中,Visual Studio 似乎正在获取 Polygon 的多个定义。因此,有必要通过将 Graph_lib 指定为命名空间来解决歧义。

指定命名空间为我解决了“多边形不明确”的错误。与 abbasi 不同的是,我没有从 FLTK 网站下载 FLTK 库。相反,我使用 vcpkg 下载并安装它。有关安装 FLTK 的更多信息,请参阅我对此 Stack Overflow question 的回答。

【讨论】:

    猜你喜欢
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多