简体   繁体   English

C++ - 错误:类尚未声明/超出范围

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

So I have two classes - Dvd and DvdGroup.所以我有两个类 - Dvd 和 DvdGroup。 DvdGroup basically manages an array of dvds and provide manipulative member functions for that class. DvdGroup 基本上管理一组 dvd 并为该类提供可操作的成员函数。 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.问题是每当我尝试使用命令 'g++ -c Dvd.Group.cc' 编译 DvdGroup.cc 时,我都会收到一堆与未声明 'Dvd' 相关的错误,我不知道为什么。

Here are some errors below:下面是一些错误:

DvdGroup.h:14:12: error: 'Dvd' has not been declared void add(Dvd*); DvdGroup.h:14:12: 错误:“Dvd”尚未声明 void add(Dvd*);

DvdGroup.h:18:3: error: 'Dvd' does not name a type Dvd* dvdCollection[MAX_DVDS]; DvdGroup.h:18:3: 错误:'Dvd' 没有命名类型 Dvd* dvdCollection[MAX_DVDS];

DvdGroup.cc: In copy constructor 'DvdGroup::DvdGroup(DvdGroup&)': DvdGroup.cc:在复制构造函数“DvdGroup::DvdGroup(DvdGroup&)”中:

DvdGroup.cc:15:6: error: 'Dvd' was not declared in this scope for(Dvd d: dvds){ DvdGroup.cc:15:6: 错误: 'Dvd' 未在此范围内声明 (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.我觉得我遗漏了一些东西,它们都可以通过一种解决方案来修复,因为它们都涉及未声明 DVD 类,但我似乎无法弄清楚是什么。 I was wondering if anyone could tell me what I'm doing wrong?我想知道是否有人可以告诉我我做错了什么? I would really appreciate any help with fixing this.我真的很感激解决这个问题的任何帮助。

DvdGroup.cc: 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: 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头文件,但是这里:

Dvd.h: 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.您需要做的是为 DvdGroup 类提供 Dvd 类定义。 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.行到 DvdGroup.h 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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