C++构建模板数组变得一团糟
好的,我之前问了一个关于使用模板类构建数组的问题,我真的不知道我在做什么。因此,我将发布我所拥有的和我所得到的错误 主DSHW.cppC++构建模板数组变得一团糟,c++,arrays,templates,c++11,C++,Arrays,Templates,C++11,好的,我之前问了一个关于使用模板类构建数组的问题,我真的不知道我在做什么。因此,我将发布我所拥有的和我所得到的错误 主DSHW.cpp #include "stdafx.h" #include <iostream> #include <vector> #include <string> #include <numeric> #include <cstdlib> #include "GroupedArray.
#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.h23:错误C4430:缺少类型说明符-假定为int。注意:C++不支持默认INT/P>
groupedarray.h34:请参阅对正在编译的类模板实例化“groupedarray”的引用
groupedarray.h25:警告C4183:“nthField”:缺少返回类型;假定为返回“int”的成员函数
groupedarray.h26:错误C4430:缺少类型说明符-假定为int。注意:C++不支持默认INT/P>
groupedarray.h32:警告C4183:“结束”:缺少返回类型;假定为返回“int”的成员函数
dshw1.cpp43:错误C2977:“GroupedArray”:模板参数太多
total_score = accumulate(quiz.nthField(SCORE_FIELD), quiz.end(), convertAndAdd);
groupedarray.h12:参见“groupedarray”的声明
dshw1.cpp43:错误C2514:“GroupedArray”:类没有构造函数
groupedarray.h12:参见“groupedarray”的声明
dshw1.cpp50:错误C2662:“GroupedArray::end”:无法将“this”指针从“GroupedArray”转换为“GroupedArray&”
dshw1.cpp50:错误C2780:“\u Ty std::accumulate\u InIt,\u InIt,\u Ty”:需要3个参数-提供4个参数
我在这里非常绝望,因此非常感谢所有帮助。我看到的一个错误是,您尝试创建GroupedArray,而GroupedArray只有一个模板参数,T.创建groupedarray模板或在创建分组数组时仅使用一个模板参数。您的问题是缺少编译器需要的一些参数
这里说,你需要指定什么类型是因为C++不支持假设它是什么/<
groupedarray.h(23): error C4430: missing type specifier -int assumed. Note: C++ does not support default-int
举个例子
nthFieldT place{//通过数组搜索参数的第一个实例
堡垒i=0;itotal_score = accumulate(quiz.nthField(SCORE_FIELD), quiz.end(), convertAndAdd);
还有更多的错误,所以我想你能理解,这些错误说明了一切,所以请仔细阅读。如果你费心查看错误消息中的行号,这些错误是不言而喻的。恩特菲尔德该怎么办?它缺少返回类型。下一行的end也有同样的问题。GroupedArray只接受一个模板参数,但是你用两个GroupedArray实例化了这个类型。对不起,我应该早点说。我的主要问题是4430错误。我知道我没有返回类型,但真正困扰我的是不理解int的错误;我会修好的。但对我来说,它不是;永远都不是解决任何问题的办法。再次阅读C4430错误-这是因为缺少函数返回类型。对于初学者来说,函数签名必须是这样的。