【问题标题】:C++ Building Template Arrays Became a MessC++ 构建模板数组变得一团糟
【发布时间】:2014-05-25 01:46:00
【问题描述】:

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

主 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++ 不支持 default-int

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

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

groupedarray.h(26):错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-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 个

我在这里变得非常绝望,因此感谢所有帮助。

【问题讨论】:

  • 如果您费心查看错误消息中的行号,这些错误是不言而喻的。 nthField 应该做什么?它缺少返回类型。下一行的end 也有同样的问题。而GroupedArray 采用单个模板参数,但您使用两个GroupedArray&lt;string, int&gt; 实例化类型。
  • 对不起,我应该早点说的。我的主要问题是 4430 错误。我知道我没有返回类型,但真正让我难过的是不理解 int 的错误。当我查看错误的类似问题时,通常所说的事情就像放置“使用命名空间std;”会修复它。然而对我来说,它不是。
  • using namespace std; 从不解决任何问题。再次阅读 C4430 错误 - 这是因为您缺少函数返回类型。函数签名必须是这样的&lt;return-type&gt; &lt;function-name&gt;(&lt;arg-types&gt;);例如int foo(int) { ... }。如果函数没有参数,arg-types 可以省略,但return-type 不能。我建议您开始阅读 book 的初学者。

标签: c++ arrays templates c++11


【解决方案1】:

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

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

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

例如这里 nthField(T place){ //通过数组搜索参数的第一个实例 for(T i=0; i

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

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

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

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

【讨论】:

    【解决方案2】:

    我看到的一个错误是您尝试创建一个 GroupedArray,而分组数组只有一个模板参数 T。要么制作 groupedarray 模板,要么制作分组数组时只使用一个模板参数。

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 2016-11-06
      • 2011-07-06
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多