C++构建模板数组成了一场混乱

C++ Building Template Arrays Became a Mess

本文关键字:一场 混乱 建模 构建 数组 C++      更新时间:2023-10-16

好的,所以我之前问了一个关于使用模板类构建数组的问题,老实说,我仍然不知道自己在做什么。所以我只会发布我所拥有的和我所得到的错误。

主DSHW.cpp

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <cstdlib>
#include "GroupedArray.h"
using namespace std;
/**
  * Add a string representing an integer to an existing integer value
  * that represents a partial sum
  * Returns the sum
  */
int convertAndAdd(int sum, string next) {
   return sum + atoi(next.c_str());
}
int main()
{
  // array of strings based on what you get if you download
// results from a quiz on Blackboard
string submission[] = {"aaa111", "Smith", "Sam",
        "Question ID 1", "To optimize the query  select * from books b, publisher     p where b.publisherID = p.id and b.subjectID <> 1 and p.city = 'Chicago' ,  (",
    "an index on the publisher table's id is only helpful if it is a hashing-based index",
    "10", "0", "",
    "Question ID 2", "Postgres (",
    "supports multiple languages for writing stored procedures",
    "10", "10", "",
    "Question ID 3", "For a business rule  object A can have several B's , (",
    "should be implemented using a table other than the table representing A's",
    "10", "10", ""
};
const int STUDENT_COLUMNS = 3;
const int NUM_QUESTIONS = 3;
const int GROUP_SIZE = 6;
const int MAX_SCORE_FIELD = 3;
const int SCORE_FIELD = 4;
GroupedArray<string, int> quiz((submission+STUDENT_COLUMNS), NUM_QUESTIONS, GROUP_SIZE);
int total_score;
int max_score;
cout << "This is a test driver for the GroupedArray class" << endl;
total_score = accumulate(quiz.nthField(SCORE_FIELD), quiz.end(), 0, convertAndAdd);
max_score = accumulate(quiz.nthField(MAX_SCORE_FIELD), quiz.end(), 0, convertAndAdd);
cout << "The score = " << total_score << endl;
cout << "The percentage = " << 100 * total_score / (float) max_score << endl;
// comment this next line out for Linux or Mac OS
system("pause");
return 0;

}

GroupedArray.h

#ifndef _GROUPEDARRAY
#define _GROUPEDARRAY
#include "stdafx.h"
#include <vector>
#include <string>
using namespace std;

template<class T>
class GroupedArray{ 
protected:  
T container;
T col;
T num;
public:
 GroupedArray(T a, T b, T c){ //constructor
    container = a;
    size = b;
    col = c;
}
 nthField(T place){  //goes through array searching for first instance of parameter
    for(T i=0; i < place; i++);
}
end(){
    for(int* it = std::begin(array); it!=std::end(array); ++it)
    {
        cout << *it << endl;
    }
    return 0;
}//takes no parameter and points to end of array.
 ~GroupedArray(); //Destrutor
};
#endif

当我尝试构建时,我会得到以下错误

groupedarray.h(23):错误C4430:缺少类型说明符-假定为int。注意:C++不支持默认的int

groupedarray.h(34):请参阅正在编译的类模板实例化"groupedarray"的参考

groupedarray.h(25):警告C4183:"nthField":缺少返回类型;假定是返回"int"的成员函数

groupedarray.h(26):错误C4430:缺少类型说明符-假定为int。注意:C++不支持默认的int

groupedarray.h(32):警告C4183:"end":缺少返回类型;假定是返回"int"的成员函数

dshw1.cpp(43):错误C2977:"GroupedArray":模板参数太多

groupedarray.h(12):参见"groupedarray"的声明

dshw1.cpp(43):错误C2514:"GroupedArray":类没有构造函数

groupedarray.h(12):请参见"groupedarray"的声明dshw1.cpp(50):错误C2662:"GroupedArray::end":无法将"this"指针从"GroupedArray"转换为"GroupedArray&"

dshw1.cpp(50):错误C2780:"_Ty std::accumulate(_InIt,_InIt,_Ty)":需要3个参数-提供4个

我在这里越来越绝望了,所以我们非常感谢所有的帮助。

您的问题是缺少编译器需要的一些参数。

这里说你需要指定什么是类型,因为c++不支持假设它是

groupedarray.h(23): error C4430: missing type specifier -int assumed. Note: C++ does not support default-int

举个例子nthField(T位置){//遍历数组搜索参数的第一个实例对于(T i=0;i<place;i++);}>

你不应该在c++中使用单词end我认为它是保留的

另一个问题是,累加只需要3个参数,而你用4个参数传递它

total_score = accumulate(quiz.nthField(SCORE_FIELD), quiz.end(), convertAndAdd);

还有更多的错误,所以我想你明白了,错误说明了一切,所以仔细阅读。

我看到的一个错误是,当分组数组只有一个模板参数T时,您试图创建GroupedArray。要么创建GroupedArray模板,要么在创建分组数组时只使用一个模板变量。