C ++错误:未知类型名称

时间:2015-04-30 21:12:54

标签: c++

所以,这是我的代码:

/****************************************************************
File:             Video.h
Description:      class declarations

Author:           David && Evan
Class:            CSCI 120
Date:             2015 May 13

We hereby certify that this program is entirely our own work.
*****************************************************************/

#ifndef VIDEO_H
#define VIDEO_H

#include <iostream>
#include <string>
#include <vector>

#include "Person.h"
#include "Date.h"

using namespace std;

enum kind {MOVIE, TELEVISION, COMPUTER};
// 'MOVIE' = standalone film of any length, whether it's part of a franchise or not
// 'TELEVISION' = episode from mini- or recurring series
// 'COMPUTER' = online or locally hosted files
/* If need be, we can extend this by adding something for analog home movies, 
i.e., camcorder tapes or 8mm film. */

namespace Vids
{

    class Video{

        public:
            Video(); // default constructor
            Video(string name, string audience, string location, vector<Person> directors,
                vector<Person> actors, Date released);
            virtual void display() = 0; // displays information for all objects of Video type
            virtual void displayAll() = 0; // displays all information for one object
            unsigned char getDirectorSize() const { return directorSize; }
            unsigned char getActorSize() const { return actorSize; }
            string getName() const { return name; }
            string getAudience() const { return audience; }
            string getLocation() const { return location; }
            Date getReleased() const { return released; }
            Date getViewed() const { return viewed; }
            string Truncate(string str, size_t width) { // shortens output
                if (str.length() > width)
                    return str.substr(0, width) + "...";
                return str;
            }   // truncate

        protected:
            short runtimeMinutes;
            /* Theoretically runtime could be unsigned, but we might eventually 
            need negatives for special values.  I doubt we'll see one needing 
            more than 32K minutes, so no worry about overflow. */
            unsigned char directorSize;
            // number of elements in each vector, shouldn't need more than 255
            unsigned char actorSize;
            string name;    // title of movie
            string audience;    // PG = "Plenty Guns", PG-13 = "13 or more guns"
            string location;
            /* Location is a catch-all field for: URL, shelf disc is on, format 
            type, name of person it is loaned to, etc. */
            vector<Person> directors(directorSize);
            /* David: I considered using other containers, but none of them 
            offered any obvious benefits over the vector. */
            vector<Person> actors(actorSize);
            Date released;
            Date viewed;
            /* 'viewed' can be used to answer the question: "What haven't i 
            watched lately?" */

    };  // end class Video

} // end namespace Vids

#endif

编译[与其他几个文件]给我这个:

$ g++ *.cpp -o Project3

In file included from Computer.cpp:12:
In file included from ./Computer.h:15:
./Video.h:68:29: error: unknown type name 'directorSize'
vector<Person> directors(directorSize);
                         ^
./Video.h:71:26: error: unknown type name 'actorSize'
vector<Person> actors(actorSize);
                      ^

directorSize声明与导演的范围相同,那么为什么编译器不能识别它呢?

2 个答案:

答案 0 :(得分:3)

该行

vector<Person> directors(directorSize);

不是声明成员变量的正确语法。

将其更改为

vector<Person> directors;

同样,改变

vector<Person> actors(actorSize);

vector<Person> actors;

假设您可以通过调用vector成员函数获取size()中的项目数,则不需要成员变量:

unsigned char directorSize;
unsigned char actorSize;

删除它们。

答案 1 :(得分:2)

嗯,这个评论很令人费解:

/* Theoretically runtime could be unsigned, but we might eventually 
        need negatives for special values.  I doubt we'll see one needing 
        more than 32K minutes, so no worry about overflow. */
        unsigned char directorSize;

unsigned char255处达到最大值,因此如果您需要最多32K,则必须使用其他类型。事实上,最好完全删除此变量,并根据需要执行directors.size()检索大小。

可以在类定义中初始化向量:

vector<Person> directors{directorSize};

但是,如果您没有在构造函数初始化列表中初始化directorSize,则会导致未定义的行为(因为您将使用未初始化的变量directorSize)。

将此更改为:

会更好
vector<Person> directors;

并且可能在构造函数初始化列表或构造函数体中,您将向此向量中添加一些项。