标签: compiler-errors

泛型方法上的多个通配符使Java编译器(和我!)非常混淆

让我们首先考虑一个简单的场景(请参阅ideone.com上的完整源代码):

import java.util.*;

public class TwoListsOfUnknowns {
    static void doNothing(List<?> list1, List<?> list2) { }

    public static void main(String[] args) {
        List<String> list1 = null;
        List<Integer> list2 = null;
        doNothing(list1, list2); // compiles fine!
    }
}
Run Code Online (Sandbox Code Playgroud)

这两个通配符是不相关的,这就是为什么你可以doNothing用a List<String>和a 调用List<Integer>.换句话说,两者?可以指代完全不同的类型.因此,以下不编译,这是预期的(也在ideone.com上):

import java.util.*;

public class TwoListsOfUnknowns2 {
    static void doSomethingIllegal(List<?> list1, List<?> list2) {
        list1.addAll(list2); // DOES NOT COMPILE!!!
            // The method addAll(Collection<? extends capture#1-of ?>)
            // in the type List<capture#1-of …
Run Code Online (Sandbox Code Playgroud)

java generics compiler-errors wildcard

58
推荐指数
1
解决办法
7197
查看次数

C#4.0中的命名参数和泛型类型推断

我一直在编程,假设在C#4.0中调用方法时,为参数提供名称不会影响结果,除非这样做是"跳过"一个或多个可选参数.

所以我发现以下行为有点惊讶:

给定一个获取a的方法Func<T>,执行它并返回结果:

public static T F<T>(Func<T> f)
{
    return f();
}
Run Code Online (Sandbox Code Playgroud)

以及上述方法可见的另一种方法:

static void Main()
{
    string s;
Run Code Online (Sandbox Code Playgroud)

调用F(没有命名参数)编译没有任何问题:

    s = F<string>(() => "hello world"); // with explicit type argument <string>
    s = F(() => "hello world"); // with type inference
Run Code Online (Sandbox Code Playgroud)

当使用命名参数时......

    s = F<string>(f: () => "hello world");
Run Code Online (Sandbox Code Playgroud)

...使用显式类型参数的上述代码行仍然可以编译而没有问题.也许并不太令人惊讶,如果安装了ReSharper,它将表明"类型参数规范是多余的".

但是,删除类型参数时...

    s = F(f: () => "hello world");
Run Code Online (Sandbox Code Playgroud)

C#编译器将报告此错误:

无法从用法推断出方法'Program.F(System.Func)'的类型参数.尝试显式指定类型参数.

对于命名参数和类型推断之间的这种交互,是否有逻辑解释?

这种行为是否记录在语言规范的某处?

我明白,根本没有必要为这个论点命名.但是,我在一个更复杂的场景中发现了这种行为,我认为在我的方法调用中为参数命名以用于内部文档目的可能是有意义的.我不是问如何解决这个问题.我试图理解一些语言的细节.

为了使事情更有趣,我发现以下所有编译都没有问题:

    Func<string> func = () => "hello world";
    s = F<string>(func);
    s = F(func); …
Run Code Online (Sandbox Code Playgroud)

c# generics type-inference compiler-errors c#-4.0

57
推荐指数
2
解决办法
2868
查看次数

编译器错误 - 找不到msgfmt命令

我正在尝试从我的共享主机更新Git.为此,我遵循以下步骤:

  1. 下载最新的Git版本
  2. 打开包装并将其放在服务器上
  3. 配置并创建Makefile - > ./configure --prefix=$HOME/dev/git/src --without-tcltk
  4. 构建包 - > make然后make install
  5. 更新PATH .bash_profile

我被困在第4点.当我运行make命令时,我得到以下内容:

user@ssh1:~/dev/git/src$ make
SUBDIR gitweb
SUBDIR ../
make[2]: ? GIT-VERSION-FILE ? est ? jour.
GEN git-instaweb
SUBDIR perl
SUBDIR git_remote_helpers
SUBDIR templates
MSGFMT po/build/locale/is/LC_MESSAGES/git.mo
/bin/sh: msgfmt: command not found
make: *** [po/build/locale/is/LC_MESSAGES/git.mo] Erreur 127
Run Code Online (Sandbox Code Playgroud)

编译器抛出msgfmt command not found错误.

我用Google搜索它,它似乎与gettext包有关.

知道如何在共享主机上修复该错误吗?

git shared-hosting compiler-errors gettext msgfmt

57
推荐指数
4
解决办法
7万
查看次数

为什么vector <bool> :: reference不会返回对bool的引用?

#include <vector>

struct A
{
    void foo(){}
};

template< typename T >
void callIfToggled( bool v1, bool &v2, T & t )
{
    if ( v1 != v2 )
    {
        v2 = v1;
        t.foo();
    }
}

int main()
{
    std::vector< bool > v= { false, true, false };

    const bool f = false;
    A a;

    callIfToggled( f, v[0], a );
    callIfToggled( f, v[1], a );
    callIfToggled( f, v[2], a );
}
Run Code Online (Sandbox Code Playgroud)

上面示例的编译会产生下一个错误:

dk2.cpp: In function 'int main()':
dk2.cpp:29:28: error: no …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors g++

56
推荐指数
5
解决办法
1万
查看次数

错误C2275:非法使用此类型作为表达式

从昨天开始,我一直面临着我的C项目的编译错误.项目本身就是创建一个可以完成一些任务的服务.

我不知道自昨天以来发生了什么变化,但今天早上,我的代码不能再编译了.

以下是我遇到的错误:

c:\path\main.c(56): error C2275: 'SERVICE_TABLE_ENTRY' : illegal use of this type as an expression
c:\program files\microsoft sdks\windows\v7.0a\include\winsvc.h(773) : see declaration of 'SERVICE_TABLE_ENTRY'
c:\path\main.c(56): error C2146: syntax error : missing ';' before identifier 'DispatchTable'
c:\path\main.c(56): error C2065: 'DispatchTable' : undeclared identifier
c:\path\main.c(56): error C2059: syntax error : ']'
c:\path\main.c(57): error C2065: 'DispatchTable' : undeclared identifier
c:\path\main.c(57): warning C4047: 'function' : 'const SERVICE_TABLE_ENTRYA *' differs in levels of indirection from 'int'
c:\path\main.c(57): warning C4024: 'StartServiceCtrlDispatcherA' : different types for formal …
Run Code Online (Sandbox Code Playgroud)

c service winapi compiler-errors visual-studio-2010

54
推荐指数
2
解决办法
7万
查看次数

接线员'??' 不能应用于'T'和'T'类型的操作数

我有以下通用方法,但VS给我一个编译错误.(运算符'??'不能应用于'T'和'T'类型的操作数

public static T Method<T>(T model) where T : new()
{
    var m = model ?? new T();
}
Run Code Online (Sandbox Code Playgroud)

有人知道为什么吗?

编辑:可能的原因是T在我的情况下可以是一个结构,并且结构是一个非可空类型?

c# generics compiler-errors

54
推荐指数
4
解决办法
4504
查看次数

当前上下文中不存在名称"__o"

我刚刚安装了Visual Studio 2015并打开了我正在处理的asp .net项目.我收到的错误很多(都完全一样)如下:

错误CS0103当前上下文中不存在名称"__o"

实际上,我没有任何名为__o的变量,代码就像魅力一样(错误无效)但令我烦恼的是,我无法看到我的代码真的有错误,因为它在这个列表的某个地方我应该查看整个清单.

在此输入图像描述

c# asp.net compiler-errors visual-studio visual-studio-2015

54
推荐指数
4
解决办法
3万
查看次数

意外的文件结束错误

我希望你能帮助我,因为我不知道发生了什么.我在尝试将Beecrypt库添加到我的项目时遇到以下错误:

致命错误C1010:查找预编译头时意外结束文件.你忘了在你的来源添加'#include"stdafx.h"'吗?

其实我没有忘记将#include"stdafx"添加到我的源代码中.编译器将错误指向此.cxx文件的末尾:

#define BEECRYPT_CXX_DLL_EXPORT

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "beecrypt/c++/security/SecureRandom.h"
#include "beecrypt/c++/security/SecureRandomSpi.h"
#include "beecrypt/c++/security/Security.h"

using namespace beecrypt::security;

SecureRandom* SecureRandom::getInstance(const String& algorithm) throw       (NoSuchAlgorithmException)
 {
Security::spi* tmp = Security::getSpi(algorithm, "SecureRandom");

assert(dynamic_cast<SecureRandomSpi*>(tmp->cspi));

SecureRandom* result = new SecureRandom(reinterpret_cast<SecureRandomSpi*>(tmp->cspi), tmp->prov, tmp->name);

delete tmp;

return result;
}

 SecureRandom* SecureRandom::getInstance(const String& type, const String& provider) throw (NoSuchAlgorithmException, NoSuchProviderException)
  {
Security::spi* tmp = Security::getSpi(type, "SecureRandom", provider);

assert(dynamic_cast<SecureRandomSpi*>(tmp->cspi));

SecureRandom* result = new SecureRandom(reinterpret_cast<SecureRandomSpi*>(tmp->cspi), tmp->prov, tmp->name);

delete tmp;

return result;
    }

   SecureRandom* SecureRandom::getInstance(const String& type, const …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors precompiled-headers visual-studio-2008 visual-c++

53
推荐指数
6
解决办法
11万
查看次数

如何告诉distutils使用gcc?

我想用Cython包装一个包含C++和OpenMP代码的测试项目,并通过setup.py文件使用distutils构建它.我的文件内容如下所示:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext


modules = [Extension("Interface",
                     ["Interface.pyx", "Parallel.cpp"],
                     language = "c++",
                     extra_compile_args=["-fopenmp"],
                     extra_link_args=["-fopenmp"])]

for e in modules:
    e.cython_directives = {"embedsignature" : True}

setup(name="Interface",
     cmdclass={"build_ext": build_ext},
     ext_modules=modules)
Run Code Online (Sandbox Code Playgroud)

-fopenmp标志与gcc一起用于编译和链接OpenMP.但是,如果我只是调用

cls ~/workspace/CythonOpenMP/src $ python3 setup.py build
Run Code Online (Sandbox Code Playgroud)

这个标志无法识别,因为编译器是clang:

running build
running build_ext
skipping 'Interface.cpp' Cython extension (up-to-date)
building 'Interface' extension
cc -Wno-unused-result -fno-common -dynamic -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c Interface.cpp -o build/temp.macosx-10.8-x86_64-3.3/Interface.o …
Run Code Online (Sandbox Code Playgroud)

python distutils compiler-errors cython

53
推荐指数
3
解决办法
4万
查看次数

为什么 javac 允许一些不可能的强制转换而不是其他的?

如果我尝试将 a 转换String为 a java.util.Date,Java 编译器会捕获错误。那么为什么编译器不将以下内容标记为错误呢?

List<String> strList = new ArrayList<>();                                                                      
Date d = (Date) strList;
Run Code Online (Sandbox Code Playgroud)

当然,JVMClassCastException在运行时抛出 a ,但编译器不会标记它。

行为与 javac 1.8.0_212 和 11.0.2 相同。

java casting compiler-errors javac

53
推荐指数
2
解决办法
2230
查看次数