7

In the following, I haven't defined the type doesntexist.

void myfunction(doesntexist argument)
{
}

GCC 4.7.2 says "error: variable or field ‘myfunction’ declared void"

My question is this: What's going through the compilers mind here to refer to the function name being void and not the argument type?

[EDIT]
Before downvoting, be aware the answer to this issue is related to the order of the errors and -Wfatal-errors stopping the more immediately relevant message from being printed. This is not simply me having a go at a slightly vague compiler message.

jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • 2
    My gcc 4.7.2 says "error: unknown type name 'doesntexist'". I think an SSCCE (http://sscce.org/) might be in order. – NPE Nov 13 '13 at 08:13
  • 1
    Please show the *complete* and *unedited* error log. – Some programmer dude Nov 13 '13 at 08:14
  • http://coliru.stacked-crooked.com/a/6bbe3a44df482b5a – Mark Garcia Nov 13 '13 at 08:14
  • @MarkGarcia If compiled with gcc, your code would result in an error, because those are c++ libraries. – Mike G Nov 13 '13 at 08:20
  • `refer to the function name being void and not the argument type?` Compiler didn't said that. It recognized `myfunction` not as function, but as variable and said that this variable declared with type `void` (you can't have variables of type `void` in C and C++). Next line compiler states exact reason of why this happened: `‘doesntexist’ was not declared in this scope` – Ivan Aksamentov - Drop Nov 13 '13 at 08:23
  • 1
    An update on my earlier comment: when I compile the code as C, I get the message I quoted eariler; when compiled as C++, the code gives the error you cite followed by `error: 'doesntexist' was not declared in this scope`. – NPE Nov 13 '13 at 08:23
  • [Compiled as C](http://coliru.stacked-crooked.com/a/056138fd14fd75e7). How do you compile? – Ivan Aksamentov - Drop Nov 13 '13 at 08:26

3 Answers3

5

Thanks, @JoachimPileborg. The unedited error log didn't contain anything useful, and it should have! The comment lead me to the issue and solution... remove -Wfatal-errors from my makefile.

19:17 >>> gcc -Wfatal-errors main.c
main.c:2:17: error: variable or field ‘myfunction’ declared void
compilation terminated due to -Wfatal-errors.

and removing -Wfatal-errors...

19:18 >>> gcc main.c 
main.c:2:17: error: variable or field ‘myfunction’ declared void
main.c:2:17: error: ‘doesntexist’ was not declared in this scope

Problem solved.


For those who say "why use -Wfatal-errors in the first place?": I usually don't want all the errors as the first can trigger the rest. In this case it looks like the errors are given out of order, or at least in an unexpected order - I assume the compiler would run into the ‘doesntexist’ was not declared error first.

jozxyqk
  • 16,424
  • 12
  • 91
  • 180
2

Its definitely not the problem with the function being void type, possibly g++ compiler poor error message schema when the function parameters consists unknown type.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
1

Hi @jozxyqk you need to specify a type for the argument, if what you have in Coliru is properly representative of your code what you need to do is provide a valid datatype for the argument, something like void myfunc(string argument) or void myfunc(int argument) etc.

There is a decent resource on datatypes here, and another here. It may be worth doing a Google search for how to use data types in c++ or similar so yu can find some reading material on them and their usage.

Linked here is a modified version which shows a string as a valid datatype for an argument and an overloaded version for an int argument.

Let me know if you need more information:)

GMasucci
  • 2,834
  • 22
  • 42