0

I'm writing a program for my class which involves using a class inside of a 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.

All other examples I've found for this error include using class B inside class A before class B is declared. 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.

The relevant portion of the polynomial header file is:

// data members
vector<Fraction> coefs;

// methods
Polynomial() = default;
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
marvelez
  • 3
  • 1
  • 3
  • 4
    You are trying to define a default constructor when you explicitly told the compiler to do it for you (`Polynomial() = default;`). – François Moisan Mar 17 '15 at 19:17
  • Can you give us a bit more to go on? That error could happen for a few reasons. Is Polynomial the class inside another class? Did you `#include` the header file in the source file? Let's see more of your code. – Matthew Moss Mar 17 '15 at 19:17
  • 2
    That constructor is completely unnecessary anyway, since `coefs` will use the default `vector` constructor when the default `Polynomial` constructor runs. – Austin Mullins Mar 17 '15 at 19:20
  • I thought the default might be the issue so I have also tried it without the coefs definition and the error still occurs, It seems the error is happening in the line above that. Fraction is the class being used inside of the struct polynomial. All files are included but that doesn't mean I included them correctly. Here are some screenshots: http://puu.sh/gEnLp/8198bc64ae.png http://puu.sh/gEnPj/e740c0a044.png The Fraction header and cpp files are almost certainly not the issue as they were provided by the instructor. – marvelez Mar 17 '15 at 19:22
  • Where do you `#include "polynomial.h"` ? – Matthew Moss Mar 17 '15 at 19:29
  • *sigh* well I knew it was something stupid. That was it Matt, thanks a bunch. Figured out in a couple seconds/minutes whats been aggravating me for a couple hours. – marvelez Mar 17 '15 at 19:35
  • Possible duplicate: [does not name a type error](http://stackoverflow.com/questions/21026047/c-errors-class-does-not-name-a-type-and-invalid-use-of-incomplete-type) – Thomas Matthews Mar 17 '15 at 19:57
  • @ThomasMatthews: Not at all. – Lightness Races in Orbit Mar 17 '15 at 20:42

1 Answers1

3

polynomial.cpp needs to include it's header:

#include "polynomial.h"

There is no implicit association between the source (.cpp file) and the header (.h) file in C++. You must include the header for the name Polynomial to be understood.

Matthew Moss
  • 1,258
  • 7
  • 16