-1

I have some basic template programming errors.

I have this code in main.cpp:

#include <string>
#include <iostream>
#include "arrapp.h"
#include <algorithm>
#include <vector>

const int max = 1000;

int main()
{
  int your_mark = 1;
  /* 2-es*/
  int s[] = {3, 2};
  array_appender<int> ia ( s, sizeof( s ) / sizeof( s[ 0 ] ) );
  for( int i = 0; i < max - 1; ++i )
  {
    ia.append( s, sizeof( s ) / sizeof( s[ 0 ] ) );
  }

  std::string hw[] = { "Hello", "World" };
  std::string langs[] = { "C++", "Ada", "Brainfuck" };
  std::string x[] = { "Goodbye", "Cruel", "World" };
  array_appender<std::string> sa ( hw, sizeof( hw ) / sizeof( hw[ 0 ] ) );
  sa.append( langs, sizeof( langs ) / sizeof( langs[ 0 ] ) );
  sa.append( x, sizeof( x ) / sizeof( x[ 0 ] ) );

  const array_appender<std::string> ha( hw, sizeof( hw ) / sizeof( hw[ 0 ] ) );


  if ( max * 2 == ia.size() && 3 == ia.at( max ) && 2 == ia.at( 3 ) &&
       &( s[ 0 ] ) == &(ia.at( max / 2 ) ) && 2 == ha.size() && 8 == sa.size() &&
       "C++" == sa.at( 2 ) && 7 == sa.at( 5 ).length() && &( ha.at( 0 ) ) == hw )
  {
    your_mark = ia.at( max + 1 );
  }
  std::cout << "Your mark is " << your_mark;
  std::endl( std::cout );
}

And I have to solve this, by writing "arrapp.h". So I created this:

#ifndef arrapp_H
#include <algorithm>
#include <list>
#include <vector>
#include <array>

template <typename T>
T const& array_appender (std::vector <T const&> v, T const& size) {
   std::vector<T*> vec [size]= {};
    for( int idx = 0; idx < size; ++idx)
    {
        std::cout << "value of v: " << v[idx] << std::endl;
        vec.add(v[idx]);
    }
   return vec;
}
#endif // arrapp_H

But If I use this i got the error main.cpp|14|error: expected ';' before 'ia'|. So this isn't working. How can I create these template things, to work? Am I misunderstood, here array_appender don't have work as I think, as I wrote?

Korte Alma
  • 168
  • 8
  • 2
    `array_appender` is a function. A function is not a class type, but you are trying to instantiate it like a class type. – An0num0us Jun 20 '18 at 19:03
  • You created a function template but are trying to use it as a class template. – Quentin Jun 20 '18 at 19:04
  • 1
    what are you acutally trying to achieve? your have more errors than the one you report. In `array_appender`: `std::vector` has no `add()` method, you cannot push a `T` into a `vector` and `vector` looks at best questionable – 463035818_is_not_an_ai Jun 20 '18 at 19:06
  • 1
    ...`std::vector vec [size]` is an array of `vectors` .... I guess there are more. Best suggestion is to [read a book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of guessing – 463035818_is_not_an_ai Jun 20 '18 at 19:08
  • @Quentin wow, what is the basic difference? I am totally in darkness at this point. – Korte Alma Jun 20 '18 at 19:08
  • `void foo(){}` is a function `struct bar {};` is a class – 463035818_is_not_an_ai Jun 20 '18 at 19:09
  • @Whatever could you explain me a bit more, please? – Korte Alma Jun 20 '18 at 19:12
  • There are really a lot of errors. Not sure it makes a good question. By example, you reuse T for the size parameter; if T is std::string ... – Michael Doubez Jun 20 '18 at 19:14
  • 2
    There is nothing more to be explained. Defining a function allows you to call it, but not instantiate. You are trying to instantiate it, which is exactly what you are not allowed to do and what the compiler is complaining about. Don't try writing generic code if you don't have the basic understanding of C++. Learn the basics of C++ and then try templates to avoid such mistakes. – An0num0us Jun 20 '18 at 19:16
  • @mdoubez well, this is true, but I'm a beginner in templates, can you explain me how to use than `T` correctly in this case? I know there are a lot of errors, but now this error is what I can't understand. – Korte Alma Jun 20 '18 at 19:21
  • 2
    Sorry, you have too much to learn and this is not the place. A piece of advise : restart from scratch and try the compilation at every line you write to get runable code. It won't teach you but it will prevent you from having such a huge number of thing to correct at once. As it is you won't even know if you made a progress. – Michael Doubez Jun 20 '18 at 19:31
  • OP needs to read a book on C++, not ask how to make a specific template function (likely a homework for a completely missed course) – SergeyA Jun 20 '18 at 19:40

2 Answers2

1

Try something simpler first. You seem to miss some basic concepts, so I will try to give you a basic example.

A template function:

template <typename T> 
T add(const T& a,const T& b) { return a+b; }

A template class:

template <typename T>
struct adder {
    T result;
    adder(const T& a, const T& b) : result(a+b) {}
};

Those can be used like this:

int a = 4;
int b = 5;
std::cout << add(a,b);

auto x = adder<int>(a,b);
std::cout << x.result;

I have to mention that I am not proud at all of this answer. You should be aware that you wont learn basics by asking questions here and to fully explain everything that enters those two simple examples I could write two chapters (well to be honest I could not otherwise I would ;). The sad truth is that you will have to read some books.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

If I understand right, you need to write array_appender that works with the main.cpp that you already have.

You have defined a template function, but you need a template class, something like this:

template<typename T>
class array_appender {
public:
    array_appender(T* array, size_t size) {
        append(array, size);
    }
    void append(T* array, size_t size) {
        // ...
    }
    T at(size_t index) {
        // ...
    }
    size_t size() {
        // ...  
    }
private:
    std::vector<T> data;
}
VLL
  • 9,634
  • 1
  • 29
  • 54