Joey
Joey

Reputation: 13

GCC template : error : expected ',' or '...' before '< ' token

I'm a new bee to C++. I'm having below errors when tying to compile below templates example copied from "Essential C++", I simply copied the example on the book.

"error : expected ',' or '...' before '< ' token" "error : 'vec' was not declared in this scope

I was wondering what could be the problem, could you help spare some time to give a hint? so much appreciate!

'''

#include <iostream>

template <typename elemType>
void display_message(const string &msg, const vector<elemType> &vec)
{
    cout << msg;
    for(int ix = 0; ix < vec.size(); ++ix)
    {
        elemType elem = vec[ix];
        cout << elem << ' ';
    }
}
int main()
{
    int size = 10;
    ocnst vector<int> ivec1 = fibon_seq(5);
    if (is_size_ok(size))
    {
        display_message(msg, size);
    }
    display_message(msg, ivec1);
    cout << "Hello world!" << endl;
    return 0;
}

'''

Upvotes: 0

Views: 894

Answers (3)

Joey
Joey

Reputation: 13

Thank you so much for your time and generous help!H How embarrassed, I did forgot to add #include since I copied the code from previous code. "essential C++" seems a good book that recommend by many ppl. I'm still trying to familalize the language. if you think there is better material, please give me a piece of advise, Thanks!

Upvotes: 0

Jose Luis German
Jose Luis German

Reputation: 51

There's a lot of issues with the code that prevent from compilation:

  1. The functions fibon_seq and is_size_ok are not defined. You have to define them in this file or include them with a #define macro.
  2. The code appears to be including the std namespace. You need to include this with using namespace std;, usually at the beginning of the code, after library includes.
  3. The variable msg is passed as an argument in a function, but it's not defined.
  4. This function call display_message(msg, size); is being passed the wrong argument. size is an integer, but the function expects vector<int>. 5.ocnst is a typo. It should be const.

Upvotes: 1

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19349

There are a few problems with your code:

  1. ocnst should be const.
  2. Missing #include <string>
  3. Missing #include <vector>

The last one is the likely cause of that particular error.

Upvotes: 0

Related Questions