简体   繁体   English

课堂上出现“未命名类型”错误

[英]“Does not name a type” error in class

I'm writing a program for my class which involves using a class inside of a struct. 我正在为我的课程编写一个程序,其中涉及在struct内部使用一个类。 When defining the struct (named polynomial) 'Polynomial does not name a type'. 定义结构(命名为多项式)时,“多项式不命名类型”。 It triggers on the first line of the default constructor: 它在默认构造函数的第一行触发:

Polynomial::Polynomial(){
    coefs = vector<Fraction>();
}

Specifically the error occurs on the "Polynomial::Polynomial(){" line. 具体来说,该错误发生在“ Polynomial :: Polynomial(){”行上。

All other examples I've found for this error include using class B inside class A before class B is declared. 我为该错误找到的所有其他示例包括在声明类B之前在类A中using class B The only member of Polynomial is a vector of class Fractions. 多项式的唯一成员是分数类的向量。 I have tried forward declaration of class Fractions and vector is included. 我试过类分数和向量的前向声明。 This is probably a rookie mistake as I am still very new to C++ classes (this being my first one) so any help would be appreciated. 这可能是一个菜鸟错误,因为我还是C ++类的新手(这是我的第一个),因此对您有所帮助。

The relevant portion of the polynomial header file is: 多项式头文件的相关部分是:

// data members
vector<Fraction> coefs;

// methods
Polynomial() = default;

polynomial.cpp needs to include it's header: polynomial.cpp需要包含其标头:

#include "polynomial.h"

There is no implicit association between the source (.cpp file) and the header (.h) file in C++. 在C ++中,源文件(.cpp文件)和头文件(.h)之间没有隐式关联。 You must include the header for the name Polynomial to be understood. 您必须包含标题才能理解Polynomial

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

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