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

C++ C++;错误:未知类型名称,c++,C++,下面是我的代码: /**************************************************************** File: Video.h Description: class declarations Author: David && Evan Class: CSCI 120 Date: 2015 May 13 We hereby cer

下面是我的代码:

/****************************************************************
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
/****************************************************************
文件:Video.h
描述:类声明
作者:大卫和埃文
类别:CSCI 120
日期:2015年5月13日
我们特此证明,该计划完全是我们自己的工作。
*****************************************************************/
#ifndef视频
#定义视频
#包括
#包括
#包括
#包括“Person.h”
#包括“Date.h”
使用名称空间std;
枚举类{电影、电视、计算机};
//“电影”=任何长度的独立电影,无论它是否是特许经营的一部分
//“电视”=迷你剧或重复剧的一集
//“计算机”=联机或本地托管的文件
/*如果需要的话,我们可以通过添加模拟家庭电影的内容来扩展这一点,
i、 例如,摄像机磁带或8mm胶片*/
命名空间视频
{
课堂录像{
公众:
Video();//默认构造函数
视频(字符串名称、字符串观众、字符串位置、矢量控制器、,
矢量演员,发布日期);
virtual void display()=0;//显示视频类型的所有对象的信息
virtual void displayAll()=0;//显示一个对象的所有信息
unsigned char getDirectorSize()常量{return directorSize;}
无符号字符getActorSize()常量{return actorSize;}
字符串getName()常量{return name;}
字符串getAudience()常量{返回访问者;}
字符串getLocation()常量{return location;}
Date getReleased()常量{return released;}
Date getViewed()常量{返回已查看;}
字符串截断(字符串str,大小\u t宽度){//缩短输出
如果(str.length()>width)
返回str.substr(0,宽度)+“…”;
返回str;
}//截断
受保护的:
短运行时间分钟;
/*理论上,运行时可以不签名,但我们最终可能
特殊值需要底片。我怀疑我们会看到需要的底片
超过32K分钟,因此无需担心溢出*/
未签名字符目录;
//每个向量中的元素数不应超过255
无符号字符化;
字符串名称;//电影的标题
字符串观众;//PG=“大量枪支”,PG-13=“13支或更多枪支”
字符串位置;
/*位置是一个包含所有内容的字段:URL、已打开托架光盘、格式
贷款对象的类型、姓名等*/
向量董事(董事);
/*大卫:我考虑过使用其他容器,但没有一个
提供了比矢量更明显的好处*/
向量参与者(actorSize);
发布日期;
查看日期;
/*“查看”可以用来回答这个问题:“我还没有看到什么
最近看了吗?”*/
}//结束类视频
}//结束命名空间Vids
#恩迪夫
而编译[使用其他几个文件]给了我这样的信息:

$ 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);
                      ^
$g++*.cpp-o项目3
在计算机中包含的文件中。cpp:12:
包含在文件中的./Computer.h:15:
./Video.h:68:29:错误:未知类型名称“directorSize”
向量董事(董事);
^
./Video.h:71:26:错误:未知类型名称“actorSize”
向量参与者(actorSize);
^
directorSize声明在与Director相同的范围内,那么为什么编译器不能识别它呢?

vector<Person> directors(directorSize);

删除它们。

好吧,这个评论令人费解:

/* 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 char
的最大值为
255
,因此如果您需要多达32K的字符,则必须使用不同的类型。事实上,最好完全删除此变量,并根据需要执行
directors.size()
来检索大小

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

vector<Person> directors{directorSize};

大概在构造函数初始值设定项列表或构造函数主体中,您将向该向量中添加一些项。

我将把您的问题归结为一个简单的例子来演示这个问题,同时应用一些工程师中流行的“m_”成员变量前缀来突出显示某个变量的“成员”

#include <vector>

class Class {
protected:
    unsigned char m_directorSize;
    std::vector<int> m_directors(m_directorSize);
};

int main()
{
    Class x;
}
编译器认为这是一个成员函数声明,这就是为什么它需要一个类型:

    std::vector<int> something(unsigned char directorSize);
如果您使用的是C++11/14:

class Foo_Sized {
    std::vector<int> m_vec = std::vector<int>(250);
};
这声明了一个整数数组,其容量为250,大小固定。它比向量快,但它总是250大。然后,您必须自己跟踪it和其他管理开销的“使用中”计数

std::vector<int> vec;
std::cout << vec.size() << '\n';    // prints 0
vec.push(10);  // add a value of 10 to the vector.
vec.push(20);  // vec is now { 10, 20 }
std::cout << vec.size() << '\n';    // prints 2
vec.push(30);  // vec is now { 10, 20, 30 }
std::cout << vec.size() << '\n';    // prints 3
std::cout << vec[0] << '\n'; // prints 10
std::cout << vec[3] << '\n'; // undefined behavior, there is no 3rd element

std::array<int, 3> arr;
std::cout << arr.size() << '\n'; // prints 3: fixed size.
arr[0] = 10;                     // can't push, fixed size.
arr[1] = 20;
std::cout << arr.size() << '\n'; // still 3, always will be.
arr[2] = 30;
std::cout << arr.size() << '\n'; // still 3, always will be.
std::cout << arr[0] << '\n';     // prints 10
std::cout << arr[3] << '\n';     // compile error: outside fixed size
std::向量向量向量机;

std::cout
directorSize
是参数名,而不是类型名。另外,您将其声明为方法,而不是字段。如果要将其声明为字段,请不要使用括号并在其他位置对其进行初始化。该大小属于成员初始值设定项列表,而不属于类成员声明。老实说,从构造器的角度来看,成员init列表中也不需要它。与
参与者相同的问题(但您可能已经知道了)。欢迎使用堆栈溢出。一般来说,当你在这里发布一个问题时,你应该尽量发布一个“简短、自包含、正确(可编译)的示例”()或尽可能接近的示例。如果您花时间尝试在这里提供一个,您会自己回答这个问题。您确定初始值设定项列表位于声明中初始化的列表之前吗?我不知道,所以我一直避免使用它。@MooingDuck对象的初始化顺序与以前相同(基类、成员、声明顺序等)。初始化器在初始化它所初始化的对象之前不会求值
vector<Person> directors;
#include <vector>

class Class {
protected:
    unsigned char m_directorSize;
    std::vector<int> m_directors(m_directorSize);
};

int main()
{
    Class x;
}
    std::vector<int> m_directors(m_directorSize);
    /typename/ /name/ ( /values/ );
    std::vector<int> something(unsigned char directorSize);
class Foo_Sized {
    std::vector<int> m_vec;
public:
    Foo() : m_vec(250)  // default construct 250 elements
    {}
};
class Foo_Sized {
    std::vector<int> m_vec = std::vector<int>(250);
};
std::array<int, 250> m_arr;
std::vector<int> vec;
std::cout << vec.size() << '\n';    // prints 0
vec.push(10);  // add a value of 10 to the vector.
vec.push(20);  // vec is now { 10, 20 }
std::cout << vec.size() << '\n';    // prints 2
vec.push(30);  // vec is now { 10, 20, 30 }
std::cout << vec.size() << '\n';    // prints 3
std::cout << vec[0] << '\n'; // prints 10
std::cout << vec[3] << '\n'; // undefined behavior, there is no 3rd element

std::array<int, 3> arr;
std::cout << arr.size() << '\n'; // prints 3: fixed size.
arr[0] = 10;                     // can't push, fixed size.
arr[1] = 20;
std::cout << arr.size() << '\n'; // still 3, always will be.
arr[2] = 30;
std::cout << arr.size() << '\n'; // still 3, always will be.
std::cout << arr[0] << '\n';     // prints 10
std::cout << arr[3] << '\n';     // compile error: outside fixed size
class Foo_Reserved {
    std::vector<int> m_vec;
public:
    Foo() : m_vec()     // default construct empty
    {
        m_vec.reserve(250); // reserve memory for 250 elements
    }
};
class Foo {
public:
    using vec_t = std::vector<int>;
protected:
    vec_t m_vec;
public:
    Foo() : m_vec()     // default construct empty
    {
    }

    size_t vecSize() const { return m_vec.size(); }
    // or, return a look-don't-touch reference to the vector
    const vec_t& getVec() const { return m_vec; }
};

Foo f{};   // C++14 initializer
f.vecSize();
f.getVec().size();