-5

Okay so I asked a question earlier about building arrays using a template class and I honestly still have no idea what I'm doing. So I'll just post what I have and the errors I got.

Main 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

And when I try to build I get the following errors

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

groupedarray.h(34) : see reference to class template instantiation 'GroupedArray' being compiled

groupedarray.h(25): warning C4183: 'nthField': missing return type; assumed to be a member function returning 'int'

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

groupedarray.h(32): warning C4183: 'end': missing return type; assumed to be a member function returning 'int'

dshw1.cpp(43): error C2977: 'GroupedArray' : too many template arguments

groupedarray.h(12) : see declaration of 'GroupedArray'

dshw1.cpp(43): error C2514: 'GroupedArray' : class has no constructors

groupedarray.h(12) : see declaration of 'GroupedArray' dshw1.cpp(50): error C2662: 'GroupedArray::end' : cannot convert 'this' pointer from 'GroupedArray' to 'GroupedArray &'

dshw1.cpp(50): error C2780: '_Ty std::accumulate(_InIt,_InIt,_Ty)' : expects 3 arguments - 4 provided

I'm getting pretty desperate here so all help is appreciated.

Community
  • 1
  • 1
  • The errors are pretty self evident if you bother looking at the line numbers in the error messages. What is `nthField` supposed to do? It lacks a return type. Same problem with `end` on the next line. And `GroupedArray` takes a single template argument, but you instantiate the type with two `GroupedArray`. – Praetorian Apr 11 '14 at 04:34
  • I'm sorry, I should have said it earlier. My main problem is the 4430 error. I know I have no return type, but what really stumps me is the error that doesn't understand int. When I looked on similar problems with the error, the normally said things like putting "using namespace std;" would fix it. Yet for me, it's not. – user3300735 Apr 11 '14 at 04:45
  • `using namespace std;` is **never** the solution to any problem. Read the C4430 error again - it is because you're missing function return types. A function signature must be like this ` ()`; for example `int foo(int) { ... }`. The `arg-types` can be omitted if the function takes no arguments, but `return-type` cannot be. I recommend you start reading a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for beginners. – Praetorian Apr 11 '14 at 04:53

2 Answers2

1

your problem is that you are missing some arguments that the compiler needs.

here it says that you need to specify what type is because c++ do not support assuming what it is

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

here for example nthField(T place){ //goes through array searching for first instance of parameter for(T i=0; i < place; i++); }

you should not use the word end in c++ i think it is reserved

another problem is that accumulate only needs 3 arguments and you pass it with 4 arguments

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

adn there are more errors so i guess you understand, the errors says it everything so read it careful.

belnar
  • 91
  • 3
0

One error I see is that you try and make a GroupedArray while grouped array only has one template argument, T. either make the groupedarray template or when making the grouped array only use one template argument.

Straw1239
  • 589
  • 2
  • 8