我不熟悉模板.我刚开始学习它.为什么我在以下程序中遇到错误?
#include <iostream>
#include <string>
using std::cout;
using std::string;
template<class C>
C min(C a,C b) {
return a<b?a:b;
}
int main()
{
string a="first string";
string b="second string";
cout<<"minimum string is: "<<min(a,b)<<'\n';
int c=3,d=5;
cout<<"minimum number is: "<<min(c,d)<<'\n';
double e{3.3},f{6.6};
cout<<"minimum number is: "<<min(e,f)<<'\n';
char g{'a'},h{'b'};
cout<<"minimum number is: "<<min(g,h)<<'\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
13 [Error] call of overloaded 'min(std::string&, std::string&)' is ambiguous
6 [Note] C min(C, C) [with C = std::basic_string<char>]
Run Code Online (Sandbox Code Playgroud)
请帮我.
我有一个问题,这不是一个真正的问题,但是让我有点好奇的东西.
我有一个有两种方法的课程.一个是静态方法,另一个是实例方法.方法具有相同的名称.
public class BlockHeader
{
public static BlockHeader Peek(BinaryReader reader)
{
// Create a block header and peek at it.
BlockHeader blockHeader = new BlockHeader();
blockHeader.Peek(reader);
return blockHeader;
}
public virtual void Peek(BinaryReader reader)
{
// Do magic.
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试构建我的项目时,我收到一条错误消息:
以下方法或属性之间的调用不明确:'MyApp.BlockHeader.Peek(System.IO.BinaryReader)'和'MyApp.BlockHeader.Peek(System.IO.BinaryReader)'
我知道方法签名实际上是相同的,但我看不出我怎么可能直接从实例成员调用静态方法.
我认为有一个很好的理由,但有谁知道这是什么原因?
我有以下代码,在.Any之后的lambda表达式下显示了一条波浪形的红线(由于System.Linq版本3.5和4.0之间的"模糊调用" - 我如何强制它使用特定版本?
它编译并运行良好.
string[] allowedExtensions = { "PNG", "JPG", "JPEG", "GIF" };
string fileExtension = (Path.GetExtension(postedFile.FileName) ?? "NULL").ToUpper().TrimStart(new[] { '.' });
if (this.MediaService.FileAllowed(postedFile)
&& allowedExtensions.Any(e => e == fileExtension))
{ ... }
Run Code Online (Sandbox Code Playgroud)
更新:
我现在已经检查了整个解决方案中的所有(60)项目,并且所有对System.dll和System.Core.dll的引用都是版本4.0 - 我真的无法理解它从哪里获得对3.5的引用.
我被这样的事实感到惊讶的是GCC并没有考虑调用foo()下面的程序含糊:
#include <iostream>
struct B1 { bool foo(bool) { return true; } };
struct B2 { bool foo(bool) { return false; } };
struct C : public B1, public B2
{
using B1::foo;
using B2::foo;
};
int main()
{
C c;
// Compiles and prints `true` on GCC 4.7.2 and GCC 4.8.0 (beta);
// does not compile on Clang 3.2 and ICC 13.0.1;
std::cout << std::boolalpha << c.foo(true);
}
Run Code Online (Sandbox Code Playgroud)
上面的函数调用true在GCC 4.7.2和GCC 4.8.0(beta)上编译并返回,而它不会在Clang …
#include <iostream>
#include <cstdint>
template<int T> void foo()
{
std::cout << "a" << std::endl;
}
template<uint8_t T> void foo()
{
std::cout << "b" << std::endl;
}
int main()
{
foo<static_cast<uint8_t>(42)> ();
foo<static_cast<int>(42)>();
return(0);
}
Run Code Online (Sandbox Code Playgroud)
知道为什么这不能按预期工作吗?
我的gcc 4.8.1抱怨一个模糊的调用,但static_cast不应该"修复"优先级规则,在这种情况下你有两种类型具有相同的优先级吗?
嗨,我的Sample中有一个简单的RestController:
@RestController
public class PersonController {
@RequestMapping(name = "/getName", method = GET)
public String getName() {
return "MyName";
}
@RequestMapping(name = "/getNumber", method = GET)
public Double getNumber(){
return new Double(0.0);
}
}
Run Code Online (Sandbox Code Playgroud)
我有SampleController用于启动SpringBoot:
@SpringBootApplication
@Controller
public class SampleController {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行SampleCotroller时,会发生以下异常:
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'personController' bean method
public java.lang.Double com.web.communication.PersonController.getNumber()
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'personController' bean method
public java.lang.String com.web.communication.PersonController.getName() mapped.
Run Code Online (Sandbox Code Playgroud)
问题出在哪里?一个RestController中不能有更多的RequestMappings? …
我正在做一些看起来像这样的包装器:
#include <iostream>
template<class T, class Value>
void Apply(void (T::*cb)(Value), T* obj, Value v)
{
(obj->*cb)(v);
}
class Foo
{
public:
void MyFunc(const int& i)
{
std::cout << i << std::endl;
}
const int& GetValue()
{
return i_;
}
private:
int i_ = 14;
};
int main()
{
Foo f;
Apply(&Foo::MyFunc, &f, f.GetValue());
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
Apply:找不到匹配的重载函数. void Apply(void (__thiscall T::* )(Value),T *,Value):模板参数Value不明确,可能是int或const int &. void Apply(void (__thiscall T::* )(Value),T *,Value):无法推断Value …c++ templates ambiguous ambiguous-call template-argument-deduction
这不是关于Windows窗体,它只是针对"背景".
当我遇到AddRange一个MenuStrip.Items需要ToolStripMenuItem投入的错误时,我正在玩Windows FormsToolStripItem
但是我已经有AddRange一个Form.Controls之前并不要求强制转换.
经过一些实验后,我设法发现当有多个重载时发生错误,AddRange所以我试图验证我的想法:
type Foo () = class end
type Bar () = inherit Foo ()
type FooCollection () = class end // not really necessary
type Test1 () =
member __.AddRange (col: FooCollection) = () // could be an int or anything instead
member __.AddRange (foos: Foo []) = ()
type Test2 () = member __.AddRange (foos: Foo []) = ()
let lst1, lst2 …Run Code Online (Sandbox Code Playgroud) 以下代码导致编译器错误,因为它是不明确的调用,但问题是如果我们使用object而不是ArrayList没有错误发生,string版本工作正常; 你对此有解释吗?
class A
{
public A(string x)
{
Console.WriteLine("string");
}
public A(ArrayList x)
{
Console.WriteLine("ArrayList");
}
}
static void Main(string[] args)
{
A o = new A(null);
}
Run Code Online (Sandbox Code Playgroud) 以下代码使用gcc和clang编译好.
template <typename T>
struct identity
{
typedef T type;
};
template <typename T>
void foo(typename identity<T>::type);
template <typename T>
void foo(T);
int main()
{
foo<int>(0);
}
Run Code Online (Sandbox Code Playgroud)
看起来重载决策是选择第一个重载(identity<T>::type一个).
有人可以解释为什么重载不是模棱两可的吗?据我所知,它们之间唯一的区别是第一个的参数是非推导的上下文而第二个的参数不是,但是因为我明确地提供了模板参数,所以我不赞成不明白为什么这很重要.
ambiguous-call ×10
c++ ×5
templates ×4
ambiguous ×2
c# ×2
c++11 ×2
overloading ×2
.net ×1
ambiguity ×1
casting ×1
controller ×1
diagnostics ×1
f# ×1
linq ×1
orchardcms ×1
reference ×1
rest ×1
spring ×1
spring-boot ×1