Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板参数推导/替换失败c++;_C++_Templates - Fatal编程技术网

C++ 模板参数推导/替换失败c++;

C++ 模板参数推导/替换失败c++;,c++,templates,C++,Templates,我有三个文件: 主要内容: #包括 #包括“Punkt.h” int main(){ 常数Punkt s1(0,1); 常数Punkt s2(-5,2); std::cout由于wsp是一个模板,解析器会混淆它是某个表达式(wsp()还是带有模板参数的函数。这就是为什么需要指定obj.template wsp()来消除这两种情况之间的歧义。在编写模板时,typename不仅仅是在模板参数前面添加的一个随机关键字,它还具有特定的含义 将模板参数视为编译时参数。与所有参数一样,它们具有类型,而名称类

我有三个文件: 主要内容:

#包括
#包括“Punkt.h”
int main(){
常数Punkt s1(0,1);
常数Punkt s2(-5,2);

std::cout由于
wsp
是一个模板,解析器会混淆它是某个表达式
(wsp<0)>()
还是带有模板参数的函数。这就是为什么需要指定
obj.template wsp()
来消除这两种情况之间的歧义。

在编写模板时,
typename
不仅仅是在模板参数前面添加的一个随机关键字,它还具有特定的含义

将模板参数视为编译时参数。与所有参数一样,它们具有类型,而名称
类型名称
意味着模板参数的类型将是某个运行时类型的名称

因此,当您声明:

template <typename T> int wsp() const;

wsp
看起来像一个值模板,但被声明为一个类型模板。@DeiDei您是正确的,他们很快就会在该问题中运行,但这不是这个问题的内容。如果这是一个重复,那是另一个问题。这不是
模板
消歧器的位置。问题在别处。
#include <iostream>
#pragma once
class Punkt{
public:
    Punkt(int x, int y){
        m_x = x;
        m_y = y;
    }

    template <typename T> int wsp() const;
private:
    int m_x;
    int m_y;

};
#include "Punkt.h"
#include <iostream>
using namespace std;
template <typename T> 
    int Punkt::wsp() const
{
    int obiekt(T);
    try{
        if (obiekt==1){
            return m_y;
        }
        if (obiekt==0){
            return m_x;
        }
        throw;
    }
    catch(...){
        std::cout << "Incorrect number" << std::endl;
    }
}
Main.cpp:46:35: error: no matching function for call to ‘Punkt::wsp() const’
   std::cout << "s1 " << s1.wsp<0>() <<  " " << s1.wsp<1>() << std::endl;

In file included from Main.cpp:39:0:
Punkt.h:11:28: note: candidate: template<class T> int Punkt::wsp() const
  template <typename T> int wsp() const;

Punkt.h:11:28: note:   template argument deduction/substitution failed:
template <typename T> int wsp() const;
template <int T> int wsp() const;