英文:
Validating argv[] input in class constructor
问题
我正在处理一个课程作业,需要创建一个结构体的堆栈。我应该动态创建一个指针数组,大小基于单个命令行输入。传递给构造函数的值应该大于2,如果不是,则设置为10。每当我将arrSize = stoi(argv[1])
传递给Class(int)
时,它都会返回我传入的值,而不进行更改。
即使我手动将int arrSize = 1
设置为1,它仍然不能正确过滤值。我认为构造函数中的if-else语句是可以的,因为我不知道另一种测试和更改传递的参数的方法;我还只能在类中有一个接受argv值作为参数的构造函数。
我觉得我可能犯了某种可悲的简单错误,但自从周四以来,我一直在试图弄清楚这个问题。我已经完成了程序的其余部分,这是我需要修复的最后一件事情。
我无法确定这个变量是否以某种奇怪的方式作为常量传递,或者构造函数是否被有效调用,或者是否完全是其他问题。非常感谢任何帮助。
类构造函数:
Stack::Stack(int arrSize){
this->top = -1;
if (arrSize < 2){
this->size = 10;
stack = new Data*[size];
}
else{
this->size = arrSize;
stack = new Data*[size];
}
}
类属性:
private:
int top;
int size;
Data **stack; // Data是一个具有int“id”和string“information”的结构体
驱动代码:
int main(int argc, char** argv) {
srand(time(NULL));
try{
if (argc > 2){
std::cout << "请在命令行中仅输入一个参数" << std::endl;
}
else{
std::string strtemp;
Data myStruct; // 初始化空结构体用于pop()
int arraySize = std::stoi(argv[1]); // 将argv[1]转换为int,存储在arraySize中
Stack stack(arraySize); // 创建Stack对象,将arraySize作为参数传递
// ...截断...
}
}
英文:
I am working on a class assignment, where I need to create a stack of structs. I am supposed to dynamically create an array of pointers, size based on a single command line input. The value passed to constructor should be greater than 2, if not, set it to 10. Whenever I pass arrSize = stoi(argv[1]) to Class(int) it returns whatever I put without changing it.
Even if I make int arrSize = 1 manually, it still does not properly filter the values. I believe if-else statements in a constructor are okay, as I am not aware of another way to test and change the passed arg; I also can only have a single constructor in the class, that takes the argv value as an argument.
I feel like I am making some sort of pathetically simple mistake, but I have been trying to figure this out since Thursday. I have finished the rest of the program, this is the last thing I need to fix.
I cannot figure out if the variable is being passed as a const in some arcane way, or if the constructor isn't being validly called, or if its something else entirely. Any help greatly appreciated.
Class constructor:
Stack::Stack(int arrSize){
this->top = -1;
if (arrSize < 2){
this->size = 10;
stack = new Data*[size];
}
else{
this->size = arrSize;
stack = new Data*[size];
}
}
Class attributes:
private:
int top;
int size;
Data **stack; //Data is a struct with int "id" and string "information"
Driver code:
int main(int argc, char** argv) {
srand(time(NULL));
try{
if (argc > 2){
std::cout << "Please enter only a single parameter in the command line" << std::endl;
}
else{
std::string strtemp;
Data myStruct; //initializing empty struct for pop()
int arraySize = std::stoi(argv[1]); //converting argv[1] to int, store in arraySize
Stack stack(arraySize); //create Stack object, passing arraySize as arg
...truncated...}
答案1
得分: 1
我发布的代码实际上没有问题,我没有正确使用size属性,这导致了初始化时出错。
我感谢所有关于更好的编码风格的建议,我会考虑它们。至于使用向量的问题,这是一个数据结构课程,所以我认为主要的重点是从计算机科学的角度来学习如何重建堆栈、链表和哈希表,而不是从编程的角度来看。
谢谢大家!
英文:
There was actually nothing wrong with the code I posted, I was not using the size attribute properly, and it was causing an error with the initialization.
I appreciate all the suggestions for better style, I will take them into account. As to the question of using vectors, this is a data structures class, so I think the main point is to learn to reconstruct stacks, linked lists, hash tables from a computer science standpoint; rather than programming standpoint.
Thank you all!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论