gpt4 book ai didi

c++ - 由于模板函数,在条件语句之外声明类对象时出现问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:59:00 25 4
gpt4 key购买 nike

对于我的 C++ 类(class),我们的任务是编写一个模板类,其类对象的类型由用户使用模板定义。

主要代码片段:

if (dataType == "1" || dataType == "int") {  
simpleVector<int> userArray;
} else if (dataType == "2" || dataType == "double") {
simpleVector<double> userArray;
} else if (dataType == "3" || dataType == "char") {
simpleVector<char> userArray;
} else if {
simpleVector<string> userArray;
}
userArray.setDefaultArray();

由此我得到错误代码 C2065 - undeclared identifier 错误。我知道为什么会收到错误,但我不知道如何在知道数据类型之前声明 userArray。

源代码:

#include <stdio.h>
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

template<class T>
class simpleVector {
public:
void setDefaultArray ();
void setArraySize (int size);
void copy (T *arr);
void desctruct ();
int getArraySize ();
T getElementAt (int index);
void fillArray();
private:
int arraySize;
T *myArray;
};

int main () {
string dataType;
int arraySize;
bool loopCondition = false;

do {
cout << "Data Type:";
cin >> dataType;
if (dataType == "1" || dataType == "2" || dataType == "3" || dataType == "4"
|| dataType == "int" || dataType == "double" || dataType == "char" || dataType == "string") {
loopCondition = false;
} else {
cout << "WARNING: invalid data type entered." << endl;
cout << "Valid entries are (1.int, 2.double, 3.char, 4.string)" << endl;
loopCondition = true;
}
} while (loopCondition);

if (true)
int num = 9;
else
int num = 7;

int num2 = num;

//simpleVector userArray; //?? Review

if (dataType == "1" || dataType == "int") {
simpleVector<int> userArray;
} else if (dataType == "2" || dataType == "double") {
simpleVector<double> userArray;
} else if (dataType == "3" || dataType == "char") {
simpleVector<char> userArray;
} else if (dataType == "4" || dataType == "char") {
simpleVector<string> userArray;
}
userArray.setDefaultArray();
cout << "Number of Inputs:";
cin >> arraySize;
userArray.setArraySize(arraySize);
userArray.fillArray();

return 0;
}

//Should call desctruct before this if reusing.
template<class T>
void simpleVector<T>::setDefaultArray() {
arraySize = 0;
myArray = NULL; //note: NULL is case sensitive (#include <stdio.h>)
}

template<class T>
void simpleVector<T>::setArraySize (int size) {
myArray = new T[size];
}

template<class T>
void simpleVector<T>::copy (T *arr) {
//ToDo
}

template<class T>
void simpleVector<T>::desctruct () {
//ToDo
}

template<class T>
int simpleVector<T>::getArraySize () {
//ToDo
}

template<class T>
T simpleVector<T>::getElementAt (int index) {
//ToDo
}

template<class T>
void simpleVector<T>::fillArray() {
cout << "Enter Array Values" << endl;
for (int i; i < arraySize; i++) {
cout << "Element " + i + ":";
cin >> myArray[i];
}
}

谢谢,

迈克

最佳答案

Eugene 的答案中的代码看起来不错,但对于学习 C++ 来说可能太复杂了?

一个非常简单的解决方案可能是这样的

  • 声明一个 vectorBase 类,它声明您在所有 vector 中需要的所有方法
  • 让模板类继承vectorBase

template class simpleVector : public vectorBase { ...

  • 然后在你的
  • 之前声明一个vectorBase类型的指针

if (dataType == "1" || dataType == "int") ...

  • 在 if block 中将新创建的 userArrays 分配给基类指针
  • 稍后,通过 baseClass 指针访问方法,这对于所有特定模板类都是相同的

关于c++ - 由于模板函数,在条件语句之外声明类对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5322189/

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