我有一些不使用-fpermissive选项而不再编译的C++代码.这是我不能分享的专有代码,但我认为我已经能够提取一个简单的测试用例来证明这个问题.这是g ++的输出
template_eg.cpp: In instantiation of 'void Special_List<T>::do_other_stuff(T*) [with T = int]':
template_eg.cpp:27:35: required from here
template_eg.cpp:18:25: error: 'next' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
template_eg.cpp:18:25: note: declarations in dependent base 'List<int>' are not found by unqualified lookup
template_eg.cpp:18:25: note: use 'this->next' instead
Run Code Online (Sandbox Code Playgroud)
所以这是产生问题的代码:
template<class T> class List
{
public:
void next(T*){
cout<<"Doing some stuff"<<endl;
}
};
template<class T> class Special_List: public List<T>
{
public:
void …Run Code Online (Sandbox Code Playgroud) 我在下面的代码中犯了一个非常愚蠢的错误:
int main(int argc, char *argv[])
{
const int ARR_SIZE = 2147483648;
int* intarr = (int *) malloc(sizeof(int) * ARR_SIZE);
for(int i =0; i < ARR_SIZE; i++) {
intarr[i] = i;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我真的应该为ARR_SIZE使用unsigned int.2147483648当分配给带符号的32位int时,确实是最大的负数.有没有充分的理由说明编译器没有(或不应该)为此发出警告.在分配大于类型最大值的正文字时,我是否需要显式强制转换?(对于我真的想要负数的情况)
(当我碰到这个时,源语言是C++,但我认为这也是一个C问题).
这是使用g ++ 6.1.1并编译如下:
g++ -Wall -g -o test_so test_so.cpp
Run Code Online (Sandbox Code Playgroud)