0

I started out with one long header file and one long source file (I inherited a messy project and wanted to get going on my work ASAP). Now it was "finished" and I decided to split it for orderliness sake (make it more manageable in case I need to update it in the future.

I separated it in stages, and all went well and at one point I keep getting error C2146 (which basically says that the type is not specified).

Before I had the following situation:

MyAlg.h

#pragma once

#include <string>
using namespace std;

struct AlgParams {
    int   param1;
    int   param2;
};

void ParseParamFile(AlgParams& params, string& file_name);

The function ParseParamFile was defined in MyAlg.cpp. The main project files were

MyApp.h

#include "MyAlg.h"

#include <string>
using namespace std;

struct AppParams {
    string     inputFileName;
    string     paramFileName;
    AlgParams  AlgPrm;          // Note that the global parameter struct has
};                              // an AlgParams

MyApp.cpp

#include "MyApp.h"

void RunMyAlg(SomeClass& data, AlgParams& params) {
    --- bla bla bla
}

void ParseCommandLine(char* argv, AppParams& params) {
    --- bla bla bla
}

int main(int argc, char* argv)
{
    AppParams  params;
    ParseCommandLine(argv, params);

    ParseParamFile(params, params.paramFileName);

    ////  some code to create and load algorithm data ////

    RunMyAlg(data, params.AlgPrm);

    return SUCCESS;
}

I then moved the definition of RunMyAlg to MyAlg.cpp (and created a declaration in MyAlg.h). Since then it won't compile, giving me:

error C2146: syntax error : missing ';' before identifier 'AlgParams' in the relevant line of MyApp.h.

What could be causing this? Is there a way to get a report of compilation order in Visual Studio Express?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Both headers need an include of `` to parse correctly so ordering of headers doesn't matter. – Mgetz Feb 19 '14 at 13:08
  • they both do - I missed that detail when writing this toy code above (now edited for clarity) – user3306334 Feb 19 '14 at 14:25
  • Those instances of `using namespace std;` in your header files have a code smell to them. Be kind and always fully qualify your use of namespaces in header files; that way you never pollute the namespaces of your header files' clients. After you make that change, you could wrap your declaration of `AlgParams` in your own namespace, or qualify it where it's used (as `::AlgParams`), and see if that clears up the problem. As for making the output more verbose, see [this answer](http://stackoverflow.com/a/7273530). – Colin Robertson Feb 19 '14 at 22:03
  • You missed a '#pragma once' directive in the MyApp.h – Andrii Jan 10 '18 at 22:25

0 Answers0