小编Jor*_*ans的帖子

VS2010 C++成员模板函数特化错误

我有以下(最小化)代码,它在VC2005中有效,但在2010年不再有效.

template <typename TDataType>
class TSpecWrapper
  { 
  public:
    typedef typename TDataType::parent_type index_type;


  public:

    template <bool THasTriangles>
    void Spec(index_type& io_index)
      { std::cout << "False version" << std::endl; }

    template <>
    void Spec<true>(index_type& io_index)
      { std::cout << "True version" << std::endl; }
  };
Run Code Online (Sandbox Code Playgroud)

似乎当"index_type"是一个依赖类型时,我总是在特化上得到一个C2770:无效的显式模板参数错误.请注意,此代码实际上足以生成错误 - 空的main足以编译它,模板甚至不需要实例化.

如果index_type不是依赖类型,它可以正常工作.任何想法为什么在VC2010中如此,如果这实际上是标准行为或错误,并且我可以解决它?

c++ templates visual-studio-2010 specialization

2
推荐指数
1
解决办法
1515
查看次数

为什么我们必须将const"type"引用而不仅仅是类型名称发送给构造函数

我试图制作一个简单的程序(是的,这是一个家庭作业),可以生成日期,像大多数普通人一样:我将我的类属性设为私有,我试图发送相同的类型,我正在工作的构造函数但编译器没有接受它,我做了一些研究,我发现在类似的情况下,人们慷慨地发送一个const"类型"引用构造器女巫对我来说意味着不太了解OOP
所以为什么我们必须发送const"类型"引用而不仅仅是构造函数的类型名称?请给我一些初学者链接或网站
是我的代码的和平:

 class Date {  
     int d ;   
     int m ;   
     int y ;   
 public :   
      Date();   
      Date(int , int , int);   
      Date(const Date &);// my question is : why do we have to write this instead of Date( Date )   
 };
Run Code Online (Sandbox Code Playgroud)

PS:对不起我的英语

c++ oop

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

"包含太多文件"错误C1014

我遇到了以下我无法解决的错误.

1>k:\school\c++\project_2\project_2\mechanic.h(8): fatal error C1014: too many include files : depth = 1024
1>  Maintenance.cpp
1>k:\school\c++\project_2\project_2\maintenance.h(8): fatal error C1014: too many include files : depth = 1024
(continues for many other files in the project)...
Run Code Online (Sandbox Code Playgroud)

项目的示例头文件:bicycle.h

//#pragma once      //Make sure it's included only once

using namespace std;
#include <iostream>

#include "Date.h"
#include "Cyclist.h"
#include "Maintenance.h"

#ifndef BICYCLE_H_
#define BICYCLE_H_

class Bicycle
{
public:
Bicycle(Date, int, int, Cyclist);
~Bicycle(void);
    Datum   getDateJoined() const;
    int getFrameSize() const;
    int getBicycleID() const;
    Cyclist getCyclist();

    void …
Run Code Online (Sandbox Code Playgroud)

c++ include visual-studio

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

为什么c ++ lambda capture不需要声明类型?

一个lambda函数:

auto wc = find_if(words.begin(), words.end(),
[sz](const string &a)    //sz does not require type declaration
{
   return a.size() >= sz;
})
Run Code Online (Sandbox Code Playgroud)

等于

class SizeComp {
    SizeComp(size_t n): sz(n) { }  // Type required here.
    bool operator()(const string &s) const { return s.size() >= sz; }
private:
    size_t sz; 
};
Run Code Online (Sandbox Code Playgroud)

为什么sz的lambda捕获不需要类型声明?

c++

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

如何在cpp中导入/包含字符串

我想使用字符串类。我还应该参与using namespace std;吗?

我以为#include <string>就足够了,但是在CLion中,只有这两个(命名空间或包含)之一不存在时,会出现一些错误。

使事情变得更复杂的是存在<string><strings.h>。有什么不同?

c++ namespaces include

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

整数溢出会影响其他变量吗?

#include <iostream>
using namespace std;

int main() {
    unsigned char char_values[2] = {0, 255};
    char_values[1] += 1;
    cout << (int)char_values[0] << endl;
    cout << (int)char_values[1] << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中,我预计:

1
0
Run Code Online (Sandbox Code Playgroud)

因为十进制中的255是二进制中的1111 1111而十进制中的255 + 1是二进制中的1 0000 0000.所以我认为因为溢出char[0]会受到影响,char[1]但结果是:

0
0
Run Code Online (Sandbox Code Playgroud)

溢出会影响其他变量吗?

c++ integer-overflow

-4
推荐指数
1
解决办法
115
查看次数