0
static void cmd_help(char *dummy)    
{

    struct command *c;
    puts("commands are:");
    c = mscp_commands;
    do {
          printf("%-8s - %s\n", c->name ? c->name : "", c->help);
    } while (c++->name != NULL);

}

struct command mscp_commands[] = {
    ....
};

I am trying to convert a program from C into C++. The qualification is that it compiles through g++;

I am getting this error:

error: use of undeclared identifier 'mscp_commands' c = mscp_commands;

I'm thinking that it has to do something with the function not being able to "see" the struct command. Can someone help please?

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
user3239138
  • 143
  • 4
  • 17
  • You have to declare an identifier *before* you use it. Here, you're declaring `mscp_commands` *after* you're trying to use it. – Crowman Jan 27 '14 at 04:48
  • possible duplicate of [What is an 'undeclared identifier' error and how do I fix it?](http://stackoverflow.com/questions/22197030/what-is-an-undeclared-identifier-error-and-how-do-i-fix-it) – sashoalm Mar 06 '14 at 08:41

2 Answers2

1

In C and C++ everything should be declared or defined before use. When compiler finds an identifier that it has not ever seen before, like your mscp_commands in c = mscp_commands; it issues an error. You need to move definition of mscp_commands up or at least declare it like

extern struct command mscp_commands[];

before using this identifier.

These languages have concept of "forward declaration". Such declarations say that name Blah is structure or enum without giving any further details. But at least this should be present. Otherwise it is a syntax error. In your example there is nothing about command.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • There is no such concept as *"forward declaration"*. The only concepts are *declaration* and *definition*. Plus, there are exceptions to your proclaimed *"Absolutely no expections"*-rule: The **declaration** `extern int i;` is sufficient to use `i`. The definition of `i` is not required to be available prior to use. Similar exceptions exist when using pointers or references to types that have only been declared, but not yet defined. – IInspectable Jun 08 '14 at 12:05
  • You are wrong. There is such concept as "forward declaration". Google it and you will have a lot of results back. For example look at http://stackoverflow.com/questions/4757565/c-forward-declaration. I just checked the C++11 standard. It mentions this concept multiple times, for example on pages 246, 368. Think about modifying or deleting your comment. – Kirill Kobelev Jun 08 '14 at 21:47
  • I wanted to say that at the point of using something compiler should know everything/enough about this something. Your example with `extern int i;` is somewhat confusing because while being syntactically a declaration, it tells everything about the object. The full definition will not bring more details. My opinion is that terminology in the standard is not perfect, this is the core root of confusion. I will rework my answer. – Kirill Kobelev Jun 08 '14 at 21:51
  • The C++ Standard does not make a difference between *class declaration* and *forward declaration*. The latter is a misnomer, and doesn't introduce a new concept. If you can provide a *forward declaration* that is not a *declaration* or a *redeclaration* I will delete my previous comment. – IInspectable Jun 08 '14 at 22:06
  • @IInspectable It doesn't? The ISO C++ standard not only uses the term 'forward declaration', section 27.3 is actually titled '27.3 Forward declarations [iostream.forward]'. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf. In 'C' the term is usually 'prototype', but in C++ it refers to having to make a declaration that is not also the definition for the purpose of a forward reference. E.g. `struct A; struct B { A* a; }; struct A { struct B* b; };` – kfsone Jun 08 '14 at 22:18
  • Forward declaration looks like `class I1;` Definition of the class looks like `class I1 { int m1, m2; ... };`. The difference is sufficient. Unfortunately I cannot delete forward declaration from the C++ standard and from the web. We better not argue... – Kirill Kobelev Jun 08 '14 at 22:22
  • @KirillKobelev You don't need the "extern" in your example, otherwise your answer is correct. – kfsone Jun 08 '14 at 22:24
  • @kfsone, thanks for the head up. At the same time `extern` will not make any harm there and my wording was `declare it like` Like means like. – Kirill Kobelev Jun 08 '14 at 22:30
0

Move

struct command mscp_commands[] = {

};

before the cmd_help function.

Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66