简体   繁体   中英

C++ - error: class has not been declared/out of scope

So I have two classes - Dvd and DvdGroup. DvdGroup basically manages an array of dvds and provide manipulative member functions for that class. The problem is whenever I try to compile DvdGroup.cc using the command 'g++ -c Dvd.Group.cc', I get a bunch of errors all related to not having 'Dvd' declared and I'm not sure why.

Here are some errors below:

DvdGroup.h:14:12: error: 'Dvd' has not been declared void add(Dvd*);

DvdGroup.h:18:3: error: 'Dvd' does not name a type Dvd* dvdCollection[MAX_DVDS];

DvdGroup.cc: In copy constructor 'DvdGroup::DvdGroup(DvdGroup&)':

DvdGroup.cc:15:6: error: 'Dvd' was not declared in this scope for(Dvd d: dvds){

I feel like I'm missing something and they could all be fixed by one solution because they all involve having the Dvd class undeclared but I can't seem to figure out what. I was wondering if anyone could tell me what I'm doing wrong? I would really appreciate any help with fixing this.

DvdGroup.cc:

#include <iostream>
using namespace std;
#include "DvdGroup.h"

DvdGroup::DvdGroup(int n){ 
  numDvds = n;
}

DvdGroup::DvdGroup(DvdGroup& dvds){ 
    numDvds = dvds.numDvds;

    for(Dvd d: dvds){
        Dvd newDvd = Dvd;
    }
}

DvdGroup::~DvdGroup(){
//code
}

void DvdGroup::add(Dvd* d){ 
//code
}

DvdGroup.h:

#ifndef DVDGROUP_H
#define DVDGROUP_H
#define MAX_DVDS 15
#include <string>
using namespace std;

class DvdGroup
{
    public:
        DvdGroup(int);
        DvdGroup(DvdGroup&);    
        ~DvdGroup();
        void add(Dvd*);

    private: 
        Dvd* dvdCollection[MAX_DVDS];
        int numDvds;

};
#endif

Don't know if the Dvd header file is needed, but here:

Dvd.h:

#ifndef DVD_H
#define DVD_H
#define MAX_DVDS 15
#include <string>

class Dvd{
  public:
    Dvd(string, int);
    void set(string, int);
    Dvd(Dvd&);
    int getYear();
    ~Dvd();
    void print();

  private:
    string title;
    int    year;
};


#endif

What you need to do is to provide Dvd class definition for DvdGroup class. It is needed to know what type of symbol is this. Solution for your problem should be addition of:

#include "Dvd.h"

line to DvdGroup.h file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM