2

this is probably an include problem, i get these errors all over the code, and not only for string identifier for example error C2146: syntax error : missing ';' before identifier 'getName' and error C2146: syntax error : missing ';' before identifier 'name'

here's an example class:

#include "stdafx.h"

class participant
{
public:
participant(int id, string name);
~participant(void);

int getId();
string getName();

private:
        int id;
    string name;
};

here's my stdafx.h file:

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include "day.h"
#include "appointment.h"
#include "extendedAppointment.h"
#include "participant.h"
#include "calendar.h"
using namespace std;

#define no_such_appointment_error 20;
#define conflicting_appointments_error 21;
#define noSuchDayError 22;
#define incorrectAppointmentError 23;
10
  • 6
    using a namespace in a header file is a terrible idea. Any file that includes your header now has using namespace std; in it (this is bad). Commented May 14, 2012 at 18:54
  • thanks, removed it. still not fixed Commented May 14, 2012 at 18:56
  • 3
    It's not directly shown to be a problem in the code you posted, since what you've posted doesn't show them being used, but macros that end in a semi-colon are nearly always going to cause problems. Commented May 14, 2012 at 18:56
  • 2
    @MichaelBurr: Well if you remove that then string immediately becomes undefined (use std::string instead). Furthermore, why are your #defines ended with semi-colons? Commented May 14, 2012 at 18:58
  • 1
    @Michael because stdafx.h is for your precompiled header file. It's included everywhere so you only want to put things in it that are needed everywhere and do not change often. If you put everything in stdafx.h anytime you change one of your headers you must recompile your entire project instead of just the bit that changed. A general rule of thumb is to include as few headers in headers as possible. Commented May 14, 2012 at 19:30

1 Answer 1

7

So I compiled your code as-posted without your custom header files and it worked just fine. Based on that, I am going to wager that you have a problem in one of these header files:

#include "day.h"
#include "appointment.h"
#include "extendedAppointment.h"
#include "participant.h"
#include "calendar.h"

It could be a macro, a class/struct not terminated with a semi-colon, etc. Check those out.

Lastly, a few of tangential issues:

First, using a namespace in a header file is a terrible idea. Any file that includes your header now has using namespace std; in it (this is bad). You probably don't want to include that many header files in every file that includes stdafx.h.

Secondly, once you remove that then string immediately becomes undefined (use std::string instead).

Last, why are your #defines ended with semi-colons? No need for that.

Sign up to request clarification or add additional context in comments.

6 Comments

targetver.h is generated by Visual Studio so we can probably assume it's not the culprit as well.
I see. thanks, i will start eliminating all the possibilities
@EdS. about string should i just replace each occurrence of string with std::string ? thats a lot of std::string to write. isn't there a better way ?
You can do a using std::string; which is slightly better than pulling in the entire namespace but most people prefer to use explicit scoping like std::string to avoid naming conflicts and for clarity.
@AJG85: It would be better, but I would never do it in a header file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.