我在声明一个使用 的函数时遇到一些麻烦boost::enable_if:下面的代码给了我一个编译器错误:
// Declaration
template <typename T>
void foo(T t);
// Definition
template <typename T>
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)
{
}
int main()
{
foo(12);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时,出现“对 foo 的调用不明确”错误。根据 的定义enable_if,“type”typedef 对应于void条件为 true 时,因此据我所知,两个签名匹配foo。为什么编译器认为它们不同,是否有正确的方法来转发声明foo(最好不要重复该enable_if部分)?
我最近开始学习C#.我刚刚了解了属性,并决定制作一个简单的程序,以便更好地理解它们.这是我写的代码:
class Dog
{
private int weight;
private string colour;
public string colour { get; set; }
public Dog(int theWeight, string theColour)
{
weight = theWeight;
colour = theColour;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个模棱两可的错误.据我所知,这不应该发生.
下面的代码不能在gcc 4.5上编译,因为对foo的调用是不明确的.消除歧义的正确方法是什么?
#include <iostream>
#include <functional>
using namespace std;
void foo(std::function<void(int, int)> t)
{
t(1, 2);
}
void foo(std::function<void(int)> t)
{
t(2);
}
int main()
{
foo([](int a, int b){ cout << "a: " << a << " b: " << b << endl;});
}
Run Code Online (Sandbox Code Playgroud) 我尝试用一个operator bool和一个创建一个类operator void*,但编译器说他们是不明确的.有什么方法可以向编译器解释使用什么操作符,或者我可以不同时使用它们?
class A {
public:
operator void*(){
cout << "operator void* is called" << endl;
return 0;
}
operator bool(){
cout << "operator bool is called" << endl;
return true;
}
};
int main()
{
A a1, a2;
if (a1 == a2){
cout << "hello";
}
}
Run Code Online (Sandbox Code Playgroud) 我有多个父母的C++课程; 每个父级定义一个具有通用名称但具有不同目的的函数:
class BaseA
{
virtual void myFunc(); // does some task
};
class BaseB
{
virtual void myFunc(); // does some other task
};
class Derived : public BaseA, public BaseB;
Run Code Online (Sandbox Code Playgroud)
如果就是这样,我就没有问题 - 我可以使用using语句解决它的歧义,我可以选择使用基类名称和范围解析运算符来调用哪一个.
不幸的是,派生类需要覆盖它们:
class Derived : public BaseA, public BaseB
{
virtual void BaseA::myFunc(); // Derived needs to change the way both tasks are done
virtual void BaseB::myFunc();
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,不是因为它引入了新的歧义(虽然它可能),但是因为
"错误C3240:'myFunc':必须是'BaseA'的非重载抽象成员函数"
"错误C2838:成员声明中的非法限定名称"
在不同的情况下,我可能只是重命名方法,或者像编译器建议的那样使它们成为纯虚方法.但是,类层次结构和许多外部问题使得第一个选项非常困难,第二个选项不可能.
有没有人有建议?为什么限定符仅允许纯虚方法?有没有办法同时覆盖虚拟方法和解决歧义?
我写了一个Haskell程序并得到了一个我不明白的编译错误.
该计划应:
StringString入NestedList数据类型NestedList成一个ListList不幸的是,由于类型模糊,它不会编译.
Haskell代码:
{-
Run like this:
$ ./prog List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]]
Output: [1,2,3,4,5]
-}
import System.Environment
import Data.List
data NestedList a = Elem a | List [NestedList a]
deriving (Read)
main = do
args <- getArgs
print . flatten . read $ intercalate " " args
flatten :: NestedList a -> [a]
flatten (Elem …Run Code Online (Sandbox Code Playgroud) 我尝试了几种不同的解析器生成器(Bison、DParser 等),它们声称能够生成 GLR 解析器,即可以处理不明确的语法的解析器。这是我正在讨论的类型的一个非常简单的二义性语法:
START: A | B;
A: C | D;
B: C | D;
C: T1 | T2;
D: T3 | T4;
T1: 't1';
T2: 't2';
T3: 't3';
T4: 't4';
Run Code Online (Sandbox Code Playgroud)
我可以很好地生成解析器,但是当我提供应该有效的解析器输入时,我会遇到“未解决的歧义”错误或直接崩溃。当我将语法更改为明确的版本时,不会出现任何类型的问题。
我对 GLR 解析器有什么不理解的地方?我认为重点是,在歧义的情况下,所有可能的解析都会被跟踪,直到它们合并或到达死胡同。我所需要的只是一个解析器,它可以告诉我输入是否有任何有效的解析。
谢谢你的帮助。
编辑:
这令人沮丧。使用 %dprec 和 %merge 我已经能够让 Bison 处理不明确的规则和终端,但它仍然对我需要处理的那种非常简单但高度病态的伪英语语法感到窒息:
S: NP VP | NPA VP;
NPA: D N | NP PP;
NP: D N | NP PP | NPA;
VP: V NP | VP PP;
PP: P NP;
D: "the" | "a";
P: "in" …Run Code Online (Sandbox Code Playgroud) 我正在复制我所面临的情况.
假设我们有一个程序集,C#类为:
public class Program
{
int n = 0;
public void Print()
{
Console.WriteLine(n);
}
public Program()
{
}
public Program(int num = 10)
{
n = num;
}
}
Run Code Online (Sandbox Code Playgroud)
我们在VB.NET项目中引用上面的程序集并尝试创建Program类的实例:
Module Module1
Sub Main()
Dim p As New Program()
p.Print()
p = New Program(20)
p.Print()
Console.ReadLine()
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)
VB.NET项目没有编译,给出错误:
'.ctor'含糊不清,因为"ConsoleApplication2.Program"类中存在多种具有此名称的成员.
从错误消息我们可以看到VB.NET编译器不确定要调用哪个构造函数 - 因为一个构造函数是无参数的,而另一个构造函数具有一个可选参数.此问题发生在VS2010/.NET 4中,而不是VS2012/.NET 4.5中.同样在C#中没有任何问题,它成功编译并运行Program类的对象初始化代码.
有没有办法在不改变构造函数的情况下在VB.NET + VS2010/.NET 4中创建Program类的实例?
我试图编译以下代码但是出错了
static void test(long... x)
{
System.out.println("long...");
}
static void test(Integer... x)
{
System.out.println("Integer...");
}
public static void main(String [] args) {
int no=5;
test(no,no);//getting error at this point in eclipse 'The method test(long[]) is ambiguous '
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它含糊不清.意味着如果我传递一个int值它应该自动框并test(Integer..x)应该被调用..类似的行test(long..x )应该被调用..这是我的理解..有人可以解释为什么它是模棱两可的?
考虑以下 Swift 表达式
println(Generic<Foo, Bar>(1))
Run Code Online (Sandbox Code Playgroud)
Normally, one would read this as a generic call to the constructor Generic<Foo, Bar> with the arguments (1).
println( Generic<Foo,Bar>(1) )
Run Code Online (Sandbox Code Playgroud)
However, when re-arranging the tokens a bit, it could also represent two separate comparisons, for example if Generic and Foo were some poorly named numeric variables:
println(Generic < Foo, Bar > (1))
// or, with proper parenthesis
println((Generic < Foo), (Bar > 1))
Run Code Online (Sandbox Code Playgroud)
What we can observe here is that an expression with …