Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:22:16+00:00 2026-05-26T02:22:16+00:00

Please consider the following three simplified files: student.h: #ifndef STUDENT_H #define STUDENT_H #include course.h

  • 0

Please consider the following three simplified files:
student.h:

#ifndef STUDENT_H
#define STUDENT_H

#include "course.h"

class Student
{
private:
    Course someCourse;
};

#endif

course.h:

#ifndef COURSE_H
#define COURSE_H

#include "student.h"

class Course
{
private:
    Student someStudent;
};

#endif

and main.cpp:

#include "student.h"
int main();

This wouldn’t compile giving me
error C2146: syntax error : missing ‘;’ before identifier ‘someStudent’

It would produce a lot more errors (even for the correct portions of code) in a more complicated program. I guess the design is wrong: Student includes Course and Course includes Student. What I want to represent with it is that a student takes several courses and a course has has several students (I use vectors in a full program, avoided them here for simplicity). Any advice how this would be possible?

Thanks in advance, Vlad.

UPDATE:
Thanks for fast replies. Forward declaration of Student class in Course class (and removing #include "student.h") seems to do the job.
Sorry, I thought it wouldn’t matter here, but in fact I am using vectors of const pointers in each of them (since a Student shouldn’t be able to control a Course and a Course shouldn’t be able to control a Student), as:

vector<const Student* const> students; // in Course class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:22:17+00:00Added an answer on May 26, 2026 at 2:22 am

    This is going circular, as long as you declare someCourse and someStudent as non-pointer members of class Student and Course respectively (as you’ve done), because the compiler sees the definition of Student, it needs to know its size which in turn means, it needs to know the size of its all members, including Course which is one of them. But to know the size of Course, it needs to know the size of Student. That becomes circular.

    So you need to break this circle, by declaring at least one of them as pointer. For example, you can do this:

    #ifndef STUDENT_H
    #define STUDENT_H
    
    //#include "course.h" //commenting out, as it is not needed!
    
    class Course; //this is called forward declaration
    
    class Student
    {
    private:
        Course *pSomeCourse; //pointer
    };
    
    #endif
    

    Also note that when you declare pSomeCourse as pointer of type Course*, you don’t need to include the header file in which Course is defined. Just forward declaration of the class Course is enough, as I did in the above code.

    The reason why it works because the size of pointer of any class is same, and the compiler need not know the size of the class, in order to know the size of the pointer of the same class. In other words, the compile can know sizeof(Course*) without even knowing sizeof(Course).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following three .NET types: I have an interface, an abstract class,
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
Please consider the following code: class Student { } enum StudentType { } static
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following simple use case: public class Foo { public virtual int
Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc
I have an app that's about presenting fictional simplified cities. Please consider the following
Please consider the following piece of code, which throws three different exceptions (namely, System.Configuration.ConfigurationErrorsException
Consider following simple example: Area.hh #pragma once class Area; #include <QScrollArea> class Area :
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.