1

i have problem with my small project. I have two classes in it. Problem:

error: 'Display' was not declared in this scope

Display is a class. Here is code:

//main.cpp
#include <iostream>
#include "Display.h"
#include "Polynomial.h"

using namespace std;

int main()
{
    Polynomial prr;
    prr.show();
    cout<<endl;
    cout<<"Enter x= ";
    int x;
    cin>>x;
    cout<<endl;

    cout<<"value for x="<<x<<endl<<"y="<<prr.value(x);

    Display aa; // this doesn't work
    //abc.show();

    return 0;
}

//Display.h
#ifndef DISPLAY_H
#define DISPLAY_H

class Display
{
    std::vector <vector <char> > graph;
    public:
        Display(int a, int b);
        //friend void lay(Polynomial abc,Display cba);
        //void show();
};

#endif // DISPLAY_H

I was thinking that maybe vectors are doing problems. I tested it without vectors, but it didn't change anthing.

//Display.cpp
#include "Display.h"
#include <iostream>

using namespace std;


Display::Display(int a, int b)
{
    //ctor
    if(a%2==0)
        a++;
    if(b%2==0)
        b++;

    vector <char> help;
    vector <char> mid;

    for(int i=0; i<b; i++)
    {
        mid.push_back('-');
        if(i==(b+1)/2)
            help.push_back('|');
        else
            help.push_back(' ');
    }

    for(int i=0; i<a; i++)
    {
        if(i==(a+1)/2)
            graph.push_back(mid);
        else
            graph.push_back(help);
    }
}

Now it's Polynomial class it's working fine, but Display class no, and i don't know why.

//Polynomial.h
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <vector>



//class Display;

class Polynomial
{...}


#endif // POLYNOMIAL_H

//Polynomial.cpp
#include "Polynomial.h"
#include <iostream>
#include <windows.h>
#include <cmath>

using namespace std;

// constructors and methods here
// everything here working fine

Edit: After few tries i am one step back, Now in Display.h i have error :

error: 'vector' does not name a type

So i included vector lib.

But it didn't help.

Blue shirt
  • 13
  • 4
  • According to your `display.h` file, you haven't defined a default constructor (or default arguments). Thus you need to call your constructor with arguments, too: `Display aa(12, 10);`. – Aconcagua Jan 31 '19 at 15:07
  • 1
    Have you tried `Display aa(0,0);` ? – Mathieu Gasciolli Jan 31 '19 at 15:08
  • 2
    Please copy-paste the entire error message (instead of paraphrasing it, or copying half of it). Based on the example, that you show here, the error should state something about `Display` not having default constructor (or, rather, not having it declared). – Algirdas Preidžius Jan 31 '19 at 15:09
  • 2
    Your Display.h shouldn't compile (e.g. using `vector` without `std::`). Either you have another Display.h in another directory that is getting included, or Display.h has unsaved changes in your editor, or your code is not the one shown here. – interjay Jan 31 '19 at 15:09
  • 1
    `include ` in `Display.h` – bolov Jan 31 '19 at 15:09
  • Most likely the issue is related to [this](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes). See [this answer](https://stackoverflow.com/a/625801/4342498) on how the code should be structured. – NathanOliver Jan 31 '19 at 15:09
  • 2
    Please read about [mcve]. If you can reproduce the error without `Polynomial` then you can remove that entirely from the example – 463035818_is_not_an_ai Jan 31 '19 at 15:10
  • @AlgirdasPreidžius this is all nothing more – Blue shirt Jan 31 '19 at 15:11
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Jan 31 '19 at 15:12
  • 1
    @Placek070 I don't believe that -- the source code you provided must fail right when trying to define `Display` as @interjay explained. – Acorn Jan 31 '19 at 15:12
  • 2
    @Placek070 If this is the single error you get (as in, there are no "File not found "`Display.h`"", before it), and this is the code you are trying to compile - I don't believe you. – Algirdas Preidžius Jan 31 '19 at 15:13
  • @Aconcagua Nothing change – Blue shirt Jan 31 '19 at 15:13
  • @AlgirdasPreidžius https://imgur.com/a/zdQ9sde look – Blue shirt Jan 31 '19 at 15:15
  • @interjay I'll change the name of class, maybe you are right – Blue shirt Jan 31 '19 at 15:19
  • 2
    @Placek070 1) What version of GCC are you using? 2) How are you compiling such a code? 3) When I try to compile the code, that is copy-pasted from your example: [I get very different list of errors](https://wandbox.org/permlink/xVI8F7ERlK7tDqzy). Which makes me think, that you aren't compiling the same code, as you are showing us. For example, did you save the files, that you are trying to compile? – Algirdas Preidžius Jan 31 '19 at 15:25
  • @AlgirdasPreidžius that is strange, but after i change the name of class, i have more errors (the same like you). – Blue shirt Jan 31 '19 at 15:32

2 Answers2

3

Error Number 1:

You defined a constructor with 2 parameters

Display(int a, int b);

But when you call

Display aa;

Compiler try to instantiate a Display object with a default constructor, that you disabled defining a custom costructor;

you have 2 possibilities:

Adding a default constructor like

Display() = default;

or

Display() { /* do whatever you want to init with default parameter */}

Instantiate your variable using the constructor you defined

Display aa{0,0};

Error number 2:

std::vector < std::vector <char> > graph;

You declared vector<char> instead of std::vector<char>

See a Live Example

Moia
  • 2,216
  • 1
  • 12
  • 34
  • i know about it, but this don't change anything and you have to use ( ) not { }. – Blue shirt Jan 31 '19 at 15:51
  • @Placek070 both your statement are wrong. That solve your issue as you can see in the live example and using brace initialization is totally fine, and a more solid syntax – Moia Jan 31 '19 at 15:55
  • 1
    Wait..You are right ! thanks, i am a newbie so please forgive me. I just forgot about std in Display.h and this ^^ – Blue shirt Jan 31 '19 at 16:02
  • @Placek070 I was already aware of your second issue (the missing std), I pointed out that too. – Moia Jan 31 '19 at 16:02
-1

One reason is that your Display class has no default constructor, considering you're creating object like Display aa; . A default constructor is the constructor that has no arguments. Default constructors are provided implicitly by compiler as synthesized default constructor only if you don't provide any constructors to your class. If you provide your own constructors to your class, you must also explicitly provide a default constructor. So in your case, you should actually create Display object like this Display aa(argument, argument); by providing arguments. However, If you want to create object like Display aa; then add either Display () { } or Display() = default; in your Display.h file.

Considering you created object like the way I described but still getting an error, another reason could be that you're not compiling the source file that contains the Display (int,int); constructor definition (not just declaration as you did in your header file) along with the source file that contains the main function. If you did that but still getting an error in compilation, then I would assume it is a compiler issue and try adding a forward declaration class Display; which should compile the code. But the definition of Display has to be within the visible range of main function otherwise a forward declaration would do nothing.

In any case, you have to make sure the definition of your class is within the visible range of the main function that creates the class object. A class type with only declaration without a definition is called incomplete type and you cannot create an object of incomplete type. So the declaration of your Display (int,int); constructor in the Display.h is not enough. You also need a definition of that within the visible range of main function. You can either do that in the same file as main, same file as header, or a separate source file (which is the best practice) that has the complete definition of Display class, its data members, and member functions. However, you must make sure to compile that source file along with the source file containing main.

Elijah Dayan
  • 442
  • 6
  • 19
  • 3
    That won't let you construct a `Display` object, and it shouldn't be needed if you include Display.h. – interjay Jan 31 '19 at 15:19