我正在尝试用c ++声明一个对象:
Polygon poly;
并且编译器告诉“Polygon是ambigious”。这是什么意思?
以下是完整代码:
#include "Graph.h"
#include "Simple_window.h"
#include "point.h"
#include "Window.h"
using namespace Graph_lib;
int main(int argc, char **argv)
{
Point tl(100,100);
Simple_window win(tl,600,400,"canvas");
Axis xa(Axis::x, Point(20,300),280 ,10, "x axis");
win.attach(xa);
win.set_label("canvas #2");
win.wait_for_button();
Axis ya(Axis::y, Point(20,300),280 ,10, "y axis");
ya.set_color(Color::cyan);
ya.label.set_color(Color::dark_red);
win.attach(ya);
win.set_label("canvas #3");
win.wait_for_button();
Function sine(sin,0,100, Point(20,150),1000,50,50);
win.attach(sine);
win.set_label("canvas #4");
win.wait_for_button();
sine.set_color(Color::blue);
Polygon poly; ///ERROR! Ambigious!!!
poly.add(Point(300,200));
poly.add(Point(350,100));
poly.add(Point(400,200));
poly.set_color(Color::red);
poly.set_style(Line_style::dash);
win.attach(poly);
win.set_label("canvas #5");
win.wait_for_button();
}
正如您所看到的,用户编写的标题之间存在冲突,而某些标题包含在某些位置。这是在最近安装了FLTK 1.3之后。
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1> Graph.cpp
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.cpp(65): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.cpp(131): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.cpp(132): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.cpp(389): warning C4800: 'void *' : forcing value to bool 'true' or 'false' (performance warning)
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.cpp(414): warning C4018: '>=' : signed/unsigned mismatch
1> GUI.cpp
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(107): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(112): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(117): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(130): warning C4018: '<' : signed/unsigned mismatch
1> c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(130) : while compiling class template member function 'Graph_lib::Vector_ref<T>::~Vector_ref(void)'
1> with
1> [
1> T=Graph_lib::Button
1> ]
1> c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(99) : see reference to class template instantiation 'Graph_lib::Vector_ref<T>' being compiled
1> with
1> [
1> T=Graph_lib::Button
1> ]
1> Simple_window.cpp
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(107): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(112): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(117): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(130): warning C4018: '<' : signed/unsigned mismatch
1> c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(130) : while compiling class template member function 'Graph_lib::Vector_ref<T>::~Vector_ref(void)'
1> with
1> [
1> T=Graph_lib::Button
1> ]
1> c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(99) : see reference to class template instantiation 'Graph_lib::Vector_ref<T>' being compiled
1> with
1> [
1> T=Graph_lib::Button
1> ]
1> test.cpp
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(107): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(112): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(117): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(36): error C2872: 'Polygon' : ambiguous symbol
1> could be 'c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wingdi.h(4548) : BOOL Polygon(HDC,const POINT *,int)'
1> or 'c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(256) : Graph_lib::Polygon'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(36): error C2872: 'Polygon' : ambiguous symbol
1> could be 'c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wingdi.h(4548) : BOOL Polygon(HDC,const POINT *,int)'
1> or 'c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(256) : Graph_lib::Polygon'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(36): error C2146: syntax error : missing ';' before identifier 'poly'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(36): warning C4551: function call missing argument list
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(36): error C2065: 'poly' : undeclared identifier
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(37): error C2065: 'poly' : undeclared identifier
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(37): error C2228: left of '.add' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(38): error C2065: 'poly' : undeclared identifier
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(38): error C2228: left of '.add' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(39): error C2065: 'poly' : undeclared identifier
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(39): error C2228: left of '.add' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(41): error C2065: 'poly' : undeclared identifier
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(41): error C2228: left of '.set_color' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(42): error C2065: 'poly' : undeclared identifier
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(42): error C2228: left of '.set_style' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\test.cpp(43): error C2065: 'poly' : undeclared identifier
1> Window.cpp
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(45): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4305: 'initializing' : truncation from 'Graph_lib::Color::Transparency' to 'char'
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(47): warning C4309: 'initializing' : truncation of constant value
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(107): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(112): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(117): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\window.cpp(74): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\window.cpp(76): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(130): warning C4018: '<' : signed/unsigned mismatch
1> c:\users\bryan\documents\visual studio 2010\projects\test\test\graph.h(130) : while compiling class template member function 'Graph_lib::Vector_ref<T>::~Vector_ref(void)'
1> with
1> [
1> T=Graph_lib::Button
1> ]
1> c:\users\bryan\documents\visual studio 2010\projects\test\test\gui.h(99) : see reference to class template instantiation 'Graph_lib::Vector_ref<T>' being compiled
1> with
1> [
1> T=Graph_lib::Button
1> ]
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 0 :(得分:2)
这意味着您已声明了两个或多个名为Polygon
的实体(类型,函数,变量等),并且编译器无法确定您在此上下文中的含义。
完整的错误消息应该告诉您哪些声明导致了歧义。没有看到它们,很难猜出如何最好地修复错误。
更新:现在您已经发布了一些代码,但没有发布完整的错误消息或有问题的声明,我猜测Polygon
在全局和Graph_lib
名称空间。然后,将整个Graph_lib
转储到全局命名空间中,使名称不明确。如果是这种情况,那么您需要指定::Polygon
或Grapg_lib::Polygon
来解决歧义。
答案 1 :(得分:0)