我想知道为什么对静态函数的调用是模糊的,即使其中一个显然不可能调用,因为它是私有的.我希望我可以使用私有/受保护的继承来帮助编译器解决歧义.
它是特定于MSVC还是以某种方式在标准中指定?
struct A
{
static int num() { return 0; }
};
struct B
{
static int num() { return 1; }
};
struct C : public A, private B
{};
int main()
{
C::num(); // Ambiguous access of num
}
Run Code Online (Sandbox Code Playgroud)
背景是我试图通过继承它来重用许多派生类(C,D,E,F,G)中的重载行为(A中的那个),以某种方式遵守不重复的规则你自己.
我刚刚写了一些具有以下结构的代码:
public void method(int x) {
//...
}
public void method(int x, String... things) {
//...
}
Run Code Online (Sandbox Code Playgroud)
我很惊讶这编译了,如果我调用的话
method(3);
Run Code Online (Sandbox Code Playgroud)
然后它会选择第一个.显然,这在某种意义上是自然选择的,但如果第一种方法不存在,这将是调用第二种方法的合理方式(使用空的varargs数组).所以它应该被认为是模糊的并产生编译时错误?
或者这被视为特例?
这样对待它似乎是错误的,因为这意味着添加新方法可能会破坏现有代码,这不是一个非常幸福的事态.
(Goodness只知道如果第一个被添加为包含第二个子类的子类的新方法,你最终会调用哪个...)
据我了解,函数名称使用的结果可能是以下之一:
\n= deleted, protected/private or, perhaps, something else) \xe2\x80\x94 the overall call is ill-formed.c++ well-formed ambiguous-call language-lawyer overload-resolution
我有一个DataTable,我试图用System.Linq.Enumerable上的AsEnumerable扩展方法枚举.问题是System.Data.DataTableExtensions上有一个同名的扩展方法.我需要在我的类中使用两个命名空间,因此删除其中一个using语句不是一个选项.
如何从System.Linq.Enumerable声明我想要AsEnumerable方法而不是System.Data.DataTableExtensions?
我必须重载移位运算符"<<",以便在控制台中写入和在二进制文件上写入.
我正在为ostream重载做好,而我在重载fstream时遇到一些问题,这里是:
在我的标题中:
friend ostream &operator<<(ostream &, const Fotografia &);
friend fstream &operator<<(fstream &, const Fotografia &);
Run Code Online (Sandbox Code Playgroud)
在我的cpp文件中:
fstream &operator<<(fstream & miofile, const Fotografia & sorgente)
{
//Open the file
miofile.open("data.dat", ios::binary | ios::app);
if(!miofile) cerr << "Can't open the file\n";
miofile << strlen(sorgente.Titolo);
miofile << endl;
miofile << sorgente.Titolo;
//I close the file
miofile.close();
return miofile;
}
Run Code Online (Sandbox Code Playgroud)
这是我面临的错误:
在函数`std :: fstream&operator <<(std :: fstream&,const Fotografia&)'中:
ISO C++ says that these are ambiguous, even though the worst conversion for the …Run Code Online (Sandbox Code Playgroud) 我的代码中有一个带有类型签名的重载函数:
void foo(std::string);
void foo(std::vector<std::string>);
Run Code Online (Sandbox Code Playgroud)
我希望foo的用户能够使用字符串或字符串列表来调用它
//Use case 1
foo("str");
//Use case 2
foo({"str1","str2","str3"});
foo({"str1","str2","str3","str4"});
Run Code Online (Sandbox Code Playgroud)
问题是当调用者将两个字符串传递给foo的初始值设定项列表时。
//Problem!
foo({"str1","str2"});
Run Code Online (Sandbox Code Playgroud)
对foo的调用是不明确的,因为它匹配两个类型签名。这是因为显然{"str1","str2"}是有效的构造函数std::string
所以我的问题是,在foo的声明或实现中我可以做些什么,以使我维护上面描述的API而不遇到这种模棱两可的构造方法。
我不想定义自己的字符串类,但是可以定义其他东西,而不必定义其他东西,vector<string>只要它可以使用字符串的初始化列表进行初始化即可。
只是出于好奇,为什么字符串构造函数会接受{"str1","str2"}?
考虑以下代码段(可在编译器epxlorer上获得):
template<typename T, typename... Args>
auto foo(Args&&... args) {}
template<typename... Args>
auto foo(Args&&... args) {}
int main() {
foo<char>('a');
}
Run Code Online (Sandbox Code Playgroud)
对于GCC而言,它可以完美编译,而对于Clang和MSVC而言,则均无法编译(编译器表示模棱两可的调用)
为什么Clang和MSVC无法如此看似明显的演绎?
编辑:GCC作为用户为我提供了预期的解决方案,是否有一种简单的方法来推动clang和msvc选择模板,而无需对原始代码进行太多更改?
I do not understand why the following is ambiguous according to compiler:
byte x = 200;
int novaCervena = Math.Min(x, 10);
Run Code Online (Sandbox Code Playgroud)
And once I add +1 to byte it is not
byte x = 200;
int novaCervena = Math.Min(x+1, 10);
Run Code Online (Sandbox Code Playgroud) 我写了一个非常简单的Haskell程序:
main = print $ sum $ map read ["55", "99", "101"]
Run Code Online (Sandbox Code Playgroud)
根据我过去的经验,我预计会出现"模糊类型"错误,因为签名sum $ map read [...]是(Read a, Num a) => a; Num是一个类,因此不能自己实现Show类.但是,程序正确输出"255".如何print确定产生输出的方法?(show也能够产生正确的结果,没有错误.)
我现在一直在使用这个查询,我很难过.我是SQL新手,我无法理解为什么我收到错误:
SELECT customer_number, first_name_initial, last_name,serve_address_1, serve_address_2, serve_city, serve_state, route_serve_zip_code, phone_number1, referral_code
FROM customer_master
INNER JOIN route_detail
ON m.customer_number=r.customer_number
WHERE (referral_code='american')
Run Code Online (Sandbox Code Playgroud)
错误
消息209,级别16,状态1,行1不明确的列名称'customer_number'.消息209,级别16,状态1,行1不明确的列名称'phone_number1'.
我已尝试按如下方式添加列,但也没有运气.任何帮助将不胜感激!
SELECT customer_number, first_name_initial, last_name,serve_address_1, serve_address_2, serve_city, serve_state, route_serve_zip_code, phone_number1, referral_code
FROM customer_master m
INNER JOIN route_detail r
ON m.customer_number=r.customer_number
WHERE (referral_code='american')
Run Code Online (Sandbox Code Playgroud) ambiguous-call ×10
c++ ×5
ambiguous ×2
c# ×2
.net ×1
byte ×1
c++11 ×1
haskell ×1
inner-join ×1
java ×1
javac ×1
overloading ×1
sql ×1
sql-server ×1
stdvector ×1
string ×1
templates ×1
types ×1
well-formed ×1