小编Ava*_*r33的帖子

在C++模板中进行名称查找

我有一些不使用-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)

c++ inheritance templates

39
推荐指数
3
解决办法
7584
查看次数

C++编译器不应该发出文字到负数转换的警告

我在下面的代码中犯了一个非常愚蠢的错误:

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)

c++

0
推荐指数
1
解决办法
147
查看次数

标签 统计

c++ ×2

inheritance ×1

templates ×1