C++构建模板数组成了一场混乱
C++ Building Template Arrays Became a Mess
好的,所以我之前问了一个关于使用模板类构建数组的问题,老实说,我仍然不知道自己在做什么。所以我只会发布我所拥有的和我所得到的错误。
主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模板,要么在创建分组数组时只使用一个模板变量。
- 使用strcpy将char数组的元素复制到另一个数组
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在 c++ 中拥有一组结构的正确方法是什么?
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- nlohmann-json将一个数组插入到另一个数组中
- 有没有一种方法可以在编译时获得作用域类名
- 如何克服提升精神AST混乱
- 为什么make_tie不是一件事
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- c++中O(n^(1/3))中一个数的除数的有效计数
- 一种在C++中读取TXT配置文件的简单方法
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 在调用接收数组的方法时,模板化数组大小是不是一种糟糕的做法
- 静态代码检查器抱怨.虚惊一场?
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- 为什么在c++中将指向对象的指针或引用返回到内存堆是一场灾难
- C++构建模板数组成了一场混乱