标签: ambiguity

C++运算符歧义

请原谅我,因为我对C++很新,但我在操作员歧义方面遇到了一些麻烦.对于我桌面上编译的代码,我认为它是特定于编译器的.但是,它无法在我的笔记本电脑上编译.我想我知道出了什么问题,但我看不到它的优雅方式.如果我犯了一个明显的错误,请告诉我.无论如何,这就是我要做的事情:

我创建了自己的Vector4类,它看起来像这样:

class Vector4
{
 private:
   GLfloat vector[4];
 ...
}
Run Code Online (Sandbox Code Playgroud)

然后我有这些运算符导致问题:

operator GLfloat* () { return vector; }

operator const GLfloat* () const { return vector; }

GLfloat& operator [] (const size_t i) { return vector[i]; }

const GLfloat& operator [] (const size_t i) const { return vector[i]; }
Run Code Online (Sandbox Code Playgroud)

我有转换运算符,以便我可以将我的Vector4类的实例传递给glVertex3fv,我有明显的原因下载.但是,涉及下载Vector4的调用对编译器来说是不明确的:

enum {x, y, z, w}
Vector4 v(1.0, 2.0, 3.0, 4.0);

glTranslatef(v[x], v[y], v[z]);
Run Code Online (Sandbox Code Playgroud)

以下是候选人:

candidate 1: const GLfloat& Vector4:: operator[](size_t) const
candidate 2: operator[](const GLfloat*, int) <built-in>
Run Code Online (Sandbox Code Playgroud)

当下载运算符已经在Vector4上定义时,为什么它会尝试将我的Vector4转换为GLfloat*?有没有一个简单的方法来解决这个问题?我只是犯了一个愚蠢的错误?在此先感谢您的帮助.

c++ opengl operators ambiguity operator-keyword

3
推荐指数
1
解决办法
2009
查看次数

C++模板和歧义问题

我有一个指针类的子集,如下所示:

template <typename T>
struct Pointer
{
     Pointer();
     Pointer(T *const x);
     Pointer(const Pointer &x);
     template <typename t>
     Pointer(const Pointer<t> &x);

     operator T *() const;
};
Run Code Online (Sandbox Code Playgroud)

最后一个构造函数的目标是允许传递Pointer一个子类,或者基本上可以隐式转换为的任何类型T *.这个实际规则只能由构造函数的定义强制执行,而编译器实际上无法通过声明单独解决它.如果我删除它,并尝试传递Pointer<Sub>给构造函数Pointer<Base>,我会得到一个编译错误,尽管可能的路径通过operator T *().

虽然它解决了上述问题,但却创造了另一个问题.如果我有一个重载函数,其中一个重载占用a Pointer<UnrelatedClass>而另一个占用Pointer<BaseClass>,并且我尝试用a调用它Pointer<SubClass>,我在两个重载之间得到一个模糊性,当然,意图是后一个重载将被调用.

有什么建议?(希望我足够清楚)

c++ templates ambiguity implicit-cast ambiguous-call

3
推荐指数
1
解决办法
1201
查看次数

C++ 函数重载类似转换

我收到一个错误,指出两个重载具有相似的转换。我尝试了太多东西,但没有任何帮助。

这是那段代码

CString GetInput(int numberOfInput, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT);
CString GetInput(int numberOfInput, string szTerminationPattern, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT);
Run Code Online (Sandbox Code Playgroud)

我不明白string怎么可能等于long

我正在使用 Visual C++ 6(是的,我知道它很旧,我正在处理遗留代码,所以我很无助)

编辑:触发错误的代码行是

l_szOption = GetInput(13, FALSE, 30 * 10);
Run Code Online (Sandbox Code Playgroud)

c++ overloading ambiguity

3
推荐指数
1
解决办法
1310
查看次数

歧义解决方案

void S(){}
struct S{};

int main(){
   S();
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,main中的表达式'S()'被视为函数调用表达式,而不是尝试创建类型为'S'的临时表达式.

C++标准的哪一部分讨论了这种表达式的解决方案,以支持函数声明?由于某种原因,我无法找到它.

c++ declaration function object ambiguity

3
推荐指数
1
解决办法
499
查看次数

编译器没有拾取歧义

我不得不花费一些时间来查找并修复我在以下代码中设法隔离的错误:

#include <iostream>

struct A
{
    std::string S;
    A(const std::string s) { S = s; }
};

void f1(A a) { std::cout << "f1:a.S = " << a.S << "\n"; }
void f1(const std::string s) { std::cout << "f1:s = " << s << "\n"; }

void f2(A a) { std::cout << "f2:a.S = " << a.S << "\n"; }

int main()
{
    f1(A("test"));
    f1(std::string("test"));

    f2(A("test"));
    f2(std::string("test"));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个错误是由f1函数创建的被忽略的(由我和编译器(?))模糊引起的:f2清楚地显示了这两者f1(A)并且f1(std::string)适用于A …

c++ ambiguity

3
推荐指数
1
解决办法
307
查看次数

varargs和歧义的概念?

这是一个例子:

package com.demo;
public class PassArray {
    static void vaTest(int... v){
        System.out.println("no of args : "+v.length+"contents: ");
        for (int x:v){
            System.out.println(x+" ");
        }
    }
    static void vaTest(boolean... v){
        System.out.println("no of args : "+v.length+"contents: ");
        for (boolean x:v){
            System.out.println(x+" ");

        }
    }
    public static void main(String args[]){

        vaTest(1,2,3);
        vaTest(true,false,true);
        vaTest();//Error:Ambiguous!
    }
}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我:

我有一些问题

1.为什么会出现模棱两可的错误?

2.i有一个Varargs参数就好

int doIt(int a,int b,int c,int... vals)
Run Code Online (Sandbox Code Playgroud)

为什么必须最后声明varargs?

  1. 什么是varargs和歧义?

java compiler-errors ambiguity variadic-functions java-5

3
推荐指数
1
解决办法
454
查看次数

一元算子模糊

查看C语法,似乎输入++i可以有2个派生:要么被视为前缀增量运算符,要么被视为2个整数提升,就像+(+i)(同样适用--i).
我错过了什么?

unary-expression:
   postfix-expression
   ++ unary-expression
   -- unary-expression
   unary-operator cast-expression
   sizeof unary-expression
   sizeof ( type-name )

unary-operator: one of
    & * + - ~ !

cast-expression:
    unary-expression
    ( type-name ) cast-expression
Run Code Online (Sandbox Code Playgroud)

c grammar ambiguity lexical-analysis unary-operator

3
推荐指数
1
解决办法
421
查看次数

调用模板函数时出现歧义

我有以下问题:

template< typename T >
class A
{};

class B
{
  public:
    template< typename... T >
    void operator()( A<T>... a )
    {
      std::cout << "A<T>\n";
    }

    template< typename callable >
    void operator()( callable f )
    {
      std::cout << "callable\n";
    }
};


int main()
{
  B      b;
  A<int> a;

  b( a );
}
Run Code Online (Sandbox Code Playgroud)

调用b( a )是模糊的 - 我期望输出A<T>,即执行第一个定义operator().有谁知道如何修理它?

c++ templates ambiguity variadic-templates c++14

3
推荐指数
1
解决办法
244
查看次数

为什么使用 std::istream_iterator 消除括号作为函数声明的歧义?

auto queue = [](string str) {
    istringstream ss(str);
    //std::copy(std::istream_iterator<string>(ss),
    //          std::istream_iterator<string>(),
    //          std::ostream_iterator<string>(std::cout, " "));

    //deque<string> q(std::istream_iterator<string>(ss), std::istream_iterator<string>{});
    deque<string> q(std::istream_iterator<string>(ss), std::istream_iterator<string>());
    return q;
};

Run Code Online (Sandbox Code Playgroud)

为什么编译器会抱怨

括号作为函数声明已消除歧义 [-Wveshing-parse]

如果我用 构建一个容器istream_iterator<string>()

和容器构造函数中的参数有什么区别吗std::copy

c++ declaration ambiguity function-declaration

3
推荐指数
1
解决办法
5325
查看次数

Scala:过度定义的模糊参考 - 最好消除歧义?

我有一个问题的后续问题,有和没有参数的重载方法定义的存在导致编译错误,这已经在这里讨论:为什么这个引用不明确?

回顾一下:

 trait A { 
    def foo( s: String ) : String
    def foo : String = foo( "foo" )
 }
 object B extends A {
    def foo( s: String ) : String = s
 } 
 B.foo     // won't compile
Run Code Online (Sandbox Code Playgroud)

导致错误消息:

 error: ambiguous reference to overloaded function
 both method foo in object B of type(s: String)String
 and method foo in trait A of type => String
 match expected type Unit
 B.foo
Run Code Online (Sandbox Code Playgroud)

一个有效的解决方案是为编译器提供预期的类型,如下所示:

 val s: String = B.foo
Run Code Online (Sandbox Code Playgroud)

不幸的是,人们可能并不总是想要引入额外的变量(例如在断言中).在上面引用的早期文章的答案中至少推荐两次的解决方案之一是使用空括号调用方法,如下所示:

 B.foo() …
Run Code Online (Sandbox Code Playgroud)

scala overloading ambiguity

2
推荐指数
1
解决办法
2664
查看次数