标签: generic-programming

CS0246:找不到类型或命名空间名称“T”。您是否缺少 using 指令或程序集引用?在 C# 中

我正在研究 c# 并且是初学者。我在创建霍夫曼树的情况下,我计算二进制文件中符号的频率(我的意思是符号重复的次数是频率)。我试图使这个“符号”适用于所有数据类型像intshortulong等等。我这样做使用泛型。

然后我尝试运行我收到 4 个错误的代码,例如:

CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference?
Run Code Online (Sandbox Code Playgroud)

我知道编译器无法识别这个“ T”,但在“ T”之前我只是使用public class Node<K>而不是public class Node<T> where T : K这样,那时错误是:

z.cs(13,23): warning CS0693: Type parameter `K' has the same name as the type parameter from outer type `shekhar_final_version_Csharp.Huffman<K>'
z.cs(10,18): (Location of the symbol related to previous warning)
Run Code Online (Sandbox Code Playgroud)

所以我不得不K用等效的“ …

c# generics algorithm generic-programming data-structures

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

"其中T:X,new()"或"其中T:new(),X"?

以下两个通用方法之间有什么区别,其中第一个方法首先写入new(),最后写入Book,反之亦然,最后一个方法.

public static void Save<T>(T target) where T : new(), Book
{
   ....
}
Run Code Online (Sandbox Code Playgroud)

public static void Save<T>(T target) where T : Book, new()
{
   ....
}
Run Code Online (Sandbox Code Playgroud)

Book是一个自定义类.

c# linq generics generic-programming

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

如果不键入别名类型的完整声明,则无法从单独文件中的类模板定义访问类型别名

我有一个类似Sample.hpp 的类模板 ,其类型别名为X

#ifndef SAMPLE_HPP
#define SAMPLE_HPP

template<typename STA, typename STB>
class Sample
{
    using X = Sample<STA,STB>;

public:
    Sample();
    inline X* GetNext() const;

private:
    X* Next;
};

#include "Sample.cpp"

#endif // SAMPLE_HPP
Run Code Online (Sandbox Code Playgroud)

定义位于Sample.cpp中。

#include "Sample.hpp"

template<typename STA, typename STB>
Sample<STA,STB>::Sample() {
    Next = nullptr;
}

template<typename STA, typename STB>
typename Sample<STA,STB>::X* Sample<STA,STB>::GetNext() const {
    return this->Next;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,还有其他方法来定义GetNext函数吗?例如,没有类型名或没有示例类模板的完整声明。当我将代码更改为

template<typename STA, typename STB>
Sample<STA,STB>* Sample<STA,STB>::GetNext() const {
    return this->Next;
} …
Run Code Online (Sandbox Code Playgroud)

c++ templates generic-programming c++11 c++14

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

如何根据值过滤记录键?

我有这个记录:

interface TheRecord extends TheRecordType {
  a: { typeA: 'string' },
  b: { typeB: 123 },
  c: { typeA: 'string' },
}

type TheRecordType = Record<string, TypeA | TypeB>

type TypeA = { typeA: string }
type TypeB = { typeB: number }
Run Code Online (Sandbox Code Playgroud)

我希望我的函数只接受值为 typeA 的键

doStuff('b'); //this should fail

function doStuff(arg: keyof FilteredForTypeA): void {
  ...
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试过滤掉它们的方法

type FilteredForTypeA = { [k in keyof TheRecord]: TheRecord[k] extends TypeA ? TheRecord[k] : never }
Run Code Online (Sandbox Code Playgroud)

generics types generic-programming typescript typescript-generics

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

Rust 的 C++ ADL 重载函数的替代方案?

有一个库提供了一个通用函数和一些要使用的实现:

#include <iostream>

namespace lib {
  struct Impl1 {};
  struct Impl2 {};

  void process(Impl1) { std::cout << 1; }
  void process(Impl2) { std::cout << 2; }

  template<typename T> void generalize(T t) { process(t); }
}
Run Code Online (Sandbox Code Playgroud)

我想通过外部代码扩展它。以下是 C++ 允许这样做的方式:

#include <lib.h> // the previous snippet

namespace client {
  struct Impl3 {};

  void process(Impl3) { std::cout << 3; }
}

int main() { // test
  lib::generalize(client::Impl3{}); // it couts 3
}
Run Code Online (Sandbox Code Playgroud)

注意:lib的代码对 的一无所知,client并且不执行动态调度。如何在我的 Rust 代码中实现相同的目标?(如果我不能,是否有类似计划?)

overloading generic-programming open-closed-principle rust argument-dependent-lookup

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

使用STL和一元函数适配仿函数检查列表成员资格

我试图编写一个简短的实用程序函数,它接受两个std :: pair项并测试它们的相等性,但忽略了元素的排序.另外(这是我遇到麻烦的地方)我写了一个函数来获取那些std :: pair项的容器并测试容器中给定对参数的成员资格.

/* A quick functor way to check the identity of the two items of a pair to see if each pair contains the same items regardless of order */
template <class T>
class EqualPairs : public std::binary_function<T,T,bool> {
  T arg2;

  public:
  explicit EqualPairs (const T& x) : arg2(x) { }

  bool operator() (const T& arg1) { 
    bool same = false;
    if (arg1 == arg2 || (arg1.first == arg2.second && arg1.second == arg2.first))
      same = true; …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl generic-programming functor

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

通用转换不起作用

我使用此代码加载数据并将它们转换为我使用泛型方法获取它的类型:

    public List<TResult> LoadFromeStreamFile<TResult>(string Location)
    {
        List<TResult> result = new List<TResult>();

        StreamReader reader = new StreamReader(Location);

        while (!reader.EndOfStream)
        {
            result.Add((TResult)reader.ReadLine());
        }

        reader.Close();

        return result;
    }
Run Code Online (Sandbox Code Playgroud)

但我在这段代码中有错误result.Add((TResult)reader.ReadLine()); 如何投射stringTResult

.net c# generics generic-programming c#-4.0

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

为什么这个通用的接口定义错了?

我正在尝试编写一个看起来像这样的界面

public interface IPropertyGroupCollection
{
    IEnumerable<IPropertyGroup> _Propertygroups { get;}
}

public interface IPropertyGroup
{
    IEnumerable<IProperty<T, U, V>> _conditions { get; }
}

public interface IProperty<T, U, V>
{
    T _p1 { get; }
    U _p2 { get; }
    V _p3 { get; }
}

public class Property<T, U, V> : IProperty<T, U, V>
{
    //Some Implementation
}
Run Code Online (Sandbox Code Playgroud)

我继续为_Conditions的可枚举定义获取编译错误.

我究竟做错了什么?Idea是实现类将提供通用属性包集合

c# generics interface generic-programming

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

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

两次继承自泛型类

我有两节课

public class Singleton<T> : MonoBehaviour   where T : Component
{
    protected static T _instance;


    public static T Instance
    {
        get
        {
            return _instance;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

public class GameManager : Singleton<GameManager>
{
    // Lots of Methods and Properties...
}
Run Code Online (Sandbox Code Playgroud)

我想创建3.从GameManager派生的类,例如ShooterGameManager.我可以创建ShooterGameManager

public class ShooterGameManager : GameManager
{}
Run Code Online (Sandbox Code Playgroud)

但是实例仍然是GameManager !!!,我尝试过用类通用类添加额外的类定义.

public class GameManager<T> : Singleton<T> where T : Component
{}
Run Code Online (Sandbox Code Playgroud)

但是这次有两个完全不同的类可用,没有从GameManager继承.我该怎么办 ???

c# singleton instance generic-programming unity-game-engine

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