C++ Compile Time Error: Expected Identifier Before Numeric Constant

C++ compile time error: expected identifier before numeric constant

You cannot do this:

vector<string> name(5); //error in these 2 lines
vector<int> val(5,0);

in a class outside of a method.

You can initialize the data members at the point of declaration, but not with () brackets:

class Foo {
vector<string> name = vector<string>(5);
vector<int> val{vector<int>(5,0)};
};

Before C++11, you need to declare them first, then initialize them e.g in a contructor

class Foo {
vector<string> name;
vector<int> val;
public:
Foo() : name(5), val(5,0) {}
};

C++ says expected identifier before numeric constant

C++03

It belongs in the constructor's mem-initializer:

class SetData
{
private:
data obj;

public:
SetData() : obj(43,"185 Awan Market","Talha")
{
}
// Rest goes here...
};

C++11

You must use a brace or equal initializer.

// Fine
data obj{43,"185 Awan Market","Talha"};
// Fine, too
data obj = data(43,"185 Awan Market","Talha"); //this is where the error happens

For why parentheses are not allowed, see the Non-static data member initializers proposal. Scroll down to "An issue raised in Kona regarding scope of identifiers"

The motivation for class-scope lookup is that we’d like to be able to
put anything in a non-static data member’s initializer that we could
put in a mem-initializer without significantly changing the semantics
(modulo direct initialization vs. copy initialization):

int x();

struct S {
int i;
S() : i(x()) {} // currently well-formed, uses S::x()
// ...
static int x();
};

struct T {
int i = x(); // should use T::x(), ::x() would be a surprise
// ...
static int x();
};

Unfortunately, this makes initializers of the “( expression-list )”
form ambiguous at the time that the declaration is being parsed:

struct S {
int i(x); // data member with initializer
// ...
static int x;
};

struct T {
int i(x); // member function declaration
// ...
typedef int x;
};

One possible solution is to rely on the existing rule that, if a
declaration could be an object or a function, then it’s a function:

struct S {
int i(j); // ill-formed...parsed as a member function,
// type j looked up but not found
// ...
static int j;
};

A similar solution would be to apply another existing rule, currently
used only in templates, that if T could be a type or something else,
then it’s something else; and we can use “typename” if we really mean
a type: Essentially

struct S {
int i(x); // unabmiguously a data member
int j(typename y); // unabmiguously a member function
};

Both of those solutions introduce subtleties that are likely to be
misunderstood by many users (as evidenced by the many questions on
comp.lang.c++ about why “int i();” at block scope doesn’t declare a
default-initialized int).

The solution proposed in this paper is to allow only initializers of
the “= initializer-clause” and “{ initializer-list }” forms. That
solves the ambiguity problem in most cases, for example:

HashingFunction hash_algorithm{"MD5"};

expected identifier or ‘(’ before numeric constant?

the problem was that I had struct evt declared in another location.

error: expected ')' before numeric constant

Unless STUDENTS is a string literal (which it isn't because you compare it to an int above) you should include it in a string using a format specifier %d, like this:

printf("ERROR: Invalid Input. Input should be from 1-%d.\n", STUDENTS);

error: expected ';', ',' or ')' before numeric constant is coming up to my code

You are using the identifier SIZE as an argument. This is also a macro that gets converted to 5 by the preprocessor. After the preprocessor applies the macros, it would look like

avg_array (int my_array[], int 5)

Since 5 is a numeric constant instead of an identifier, it generates an error. Change the variable name.

It looks like you also have a function signature missing a return type, which should match the prototype declared above. Try this instead:

int avg_array (int *my_array, int size)
{
int sum = 0;
int i;

for(i=0; i<size; i++)
{
sum = sum + my_array[i];
}
return sum/size;
}

The variable sum should be initialized to 0. The local variable fxn_average is not needed because you can use just return sum/size; at the end instead.


I changed the type of the first argument from int[] (array of int) to int * (pointer to int) so the function definition matches the prototype given in the question. The function was declared as

int avg_array (int*, int);

These arguments have no identifiers; only their types are specified. This is valid C, but many style guides prescribe against it since naming arguments helps the reader understand meaning or intent. If you are writing a programming interface, for example, all the programmer will likely see is the function prototypes in a header file. It must be clear what the arguments are to write a correct function call. Anyway, adding in identifiers looks like:

int avg_array (int *my_array, int size);

which is the same as in the definition I used above.

Creating vector of sets as a class member gives error in c++

Use this:

vector<set<int> > rowset = vector<set<int> >(9, set<int>());

...or that:

vector<set<int> > rowset{vector<set<int> >(9, set<int>())};

More information on these questions, similar to yours:

  • C++ compile time error: expected identifier before numeric constant
  • Why can in-class initializers only use = or {}?


Related Topics



Leave a reply



Submit