【发布时间】: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 指令会导致歧义。如果是这种情况,请使用限定名称::Polygon或Graph_lib::Polygon,具体取决于您的需要。 -
@Peter:我的 include 目录中有代码中提到的所有头文件(“Simple_window.h”和“Graph.h”)。
-
@abbasi:这意味着要么你没有实现
Graph_lib::Window::draw(),要么你没有链接到包含实现的文件或库。
标签: c++ user-interface