0

I have been reading on the forum many times before but today I cannot figure out why I am having an error in my code so if anyone can point out to why I am getting these errors I would be thankful. I am new to template and do not understand why vectr is being declare more than once here. I tried to declare vectr globally to fix the error but I do not feel that is the correct fix.

main.cpp:8:31: error: invalid declarator before _v_class printContent (vector<T> v)
                           ^
main.cpp:8:31: error: expected _)_ before _v_
main.cpp: In function _int main()_:
main.cpp:70:23: error: conflicting declaration _printContent vectr_printContent(vectr);
                   ^
main.cpp:49:20: error: _vectr_ has a previous declaration as _std::vector<double> vectr_
 vector<double> vectr;

Here are my codes, the goal is to run length encode the contents of a vector to a new vector so that the new vector would have pairs of the number of the items in the old vector plus the item. I will swap the new vector out after I manage to create it inside of this function.

#include <iostream>
#include "vector"

using namespace std;

template<class T>
class printContent (vector<T> v) //Error 31 here
{

vector<pair<int, T> > vp; //declare a new vector with type T


...(Here vectr would go through a loop and vp would fill up with pairs, for now I will also print out the content of the new vector here instead of swapping it out.) 

}

int main()
{

vector<double> vectr; //Error 49

/*... (I fill in vectr with various numbers here, could be char or int if I declare vectr to be    char or int instead)*/

printContent(vectr); //Error 70
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rooth
  • 5
  • 2
  • 1
    I guess file `"vector"` already has variable and functions with those names already. What happens when you change the names? – David G Oct 10 '14 at 21:05
  • Is `"#include "vector"` supposed to be the standard header `` or something else? If the latter, show that code too (and consider using a different name). – Michael Burr Oct 10 '14 at 21:24

1 Answers1

0

Not sure if you want a template class or are actually looking for a function to print the vector? Here is your code reworked to compile.

Source

#include <vector>
#include <utility>

using namespace std;

template< class T >
class PrintContent
{
 public:
  PrintContent( vector< T > value )
  {
     //make vector of pairs here.
  }
 private:
  vector< pair< int, T > > m_values;
};

int main( int, char** )
{
  vector< double > value;

  PrintContent< double > object( value );

  return 0;
}

Build

g++ -o homework homework.cpp

Ben Crowhurst
  • 8,204
  • 6
  • 48
  • 78
  • Ah you are right, I initially make a function to simply print out the vector but I changed it to a class without changing the content because I was caught up in template. One question though, why did you call print content as a double? I will try to implement my code and see how it work. – Rooth Oct 10 '14 at 21:26
  • I changed mine code into an actual function as I think a class is overdone here. I also find out that I was missing utility and that causes some errors. The function now is type void and creates a vector of pairs. I will now try to swap out the function or find a way to just return it. Thank you for helping me. – Rooth Oct 10 '14 at 21:45