gpt4 book ai didi

C++设置空指针?

转载 作者:行者123 更新时间:2023-11-30 02:10:57 24 4
gpt4 key购买 nike

我如何将其设置为空;

LookupTable<Product *> table;

我尝试的一切都说

1 IntelliSense:不存在合适的构造函数来将“int”转换为“LookupTable”

查找表是这样的:

#ifndef LOOKUPTABLE_H
#define LOOKUPTABLE_H

#include <iostream>
#include <string>

using namespace std;

#define MAXRANGE 10

template <class T>
class LookupTable
{
private:
T *aptr[MAXRANGE];
int rangeStart[MAXRANGE];
int rangeEnd[MAXRANGE];
int numRanges;

public:
T defaultValue;
bool failedRangeCheck;
std::string failReason;


// Constructor
LookupTable()
{
numRanges = 0;
defaultValue = T();
}

void addRange(int start, int end)
{
std::cout << "Created a new range... Start: " << start << " / End: " << end << endl;
failedRangeCheck = false;

//lines omitted because not working anyway

if ( !failedRangeCheck )
{
//set ranges
rangeStart[numRanges] = start;
rangeEnd[numRanges] = end;

//build new generic array with end-start+1 positions
//set pointer to point to it
aptr[numRanges] = new T[ end - start + 1 ];
numRanges++;
}
else
{
std::cout << "Range overlapped another range." << endl;
std::cout << failReason << endl;
}
}

T &operator[](int value) // Overloaded [] operator
{
for ( int i = 0; i < numRanges; i++ )
{
if ( (value >= rangeStart[i]) && (value <= rangeEnd[i]) )
{
return aptr[i][value - rangeStart[i]];
}
}

return defaultValue;
}

~LookupTable()
{
delete[] aptr;
numRanges = 0;
}

};
#endif

最佳答案

在您的示例中,table不是指针。它是一个 LookupTable 类型的对象,其中 LookupTable 是一个模板类。在这种情况下,您恰好正在制作一个专门用于 Product * 的 LookupTable。 s,但如果你这样做,你会得到同样的错误 LookupTable< int > table = 0;

关于C++设置空指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4093095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com