0

I have been trying to do the following to no avail,

In 'used.h',

#ifndef USED_H_
#define USED_H_
#include<iostream>
#include<string>
    class used
    {
    public:
      int member=0;
      used();
      virtual ~used();
    };
#endif

In the used.cc,

#include "used.h"
used::used()
{
}
used::~used()
{
}

In 'the_user.h',

#ifndef THE_USER_H_
#define THE_USER_H_
#include<queue>
#include<iostream>
    class used;      //Class forward declaring
    class the_user
    {
    public:
      std::deque<used*> my_queue;
      the_user();
      ~the_user();
    };
#endif

Now, I want to access and change 'member' in 'the_user.cc',

    #include "used.h"
    #include "the_used.h"
    #include<iostream>
    #include<queue>
    using namespace std;
    the_user::the_user()
    {
        deque <used*> my_queue;
        my_queue.resize(6);
        used* object = new used;       <-------marked line
        for(unsigned int i=0; i<my_queue.size(); i++)
        {
           my_queue.push_back(object);
        }

        cout << my_queue[5] << endl;                     //Should give 0
        my_queue[0]->member=1000;
        cout << my_queue[0]->member << endl;             //1000
    }

in main file(I have only read access),

#include "the_used.h"
#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;
int main()
{
  the_used *object = new the_used();
}

Actually, I am getting undefined reference to used::used() at the marked line. What seems to be the problem? I have tried to use the same for a vector as well but to no avail. I am not allowed to make changes to the 'int main(){}'. Any help would be highly appreciated.

onie
  • 13
  • 4
  • Ooops. Ignore my old comment. – hegel5000 Apr 06 '18 at 23:20
  • You're not likely to see that error unless you have some declaration of a default constructor of `used`, which seems to be omitted in your summary. The answer will depend on exactly how and where you declare and/or define that default constructor. – aschepler Apr 06 '18 at 23:29
  • Isn't class used() required for prototyping in the_used.h file? – onie Apr 06 '18 at 23:47
  • I am sorry, this is right in my actual code. I have corrected this here. Thanks for correcting me. However, the above problem is still there. If I remove the constructor altogether, I get `terminate called after throwing an instance of std::bad_alloc`. – onie Apr 07 '18 at 00:05
  • 1
    Did you try stepping through it in a debugger? If you had, you probably would have noticed that the loop is running much more than 6 times - then you need to figure out why that is. (Also, why are you expecting `my_queue[5]` to be 0 instead of a random pointer value for something you allocated?) – Daniel Schepler Apr 07 '18 at 00:09
  • Thanks evryone for the help. The problem is still there. I tried running with GDB but couldn't catch anything. I got `bad alloc` when I comment out the `used` constructor, and `undefined reference` if I keep the constructor. – onie Apr 07 '18 at 00:19
  • You get that because your loop runs forever, increasing the size of my_queue until it runs out of memory. (Which you would have noticed if you actually did debug your code. Look up how to debug code, especially setting breakpoints and stepping through a running application line by line.) – Max Vollmer Apr 07 '18 at 00:22
  • How do you compile/link your program? `undefined reference` looks like this: https://stackoverflow.com/questions/12573816 – chtz Apr 08 '18 at 08:23
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – chtz Apr 08 '18 at 08:24

1 Answers1

0

Your class declaration doesn't declare any constructor or destructor:

class used
{
public:
    int member=0;
};

But in your cpp file you define them. Your compiler should complain already here:

#include "used.h"
used::used()
{
}

used::~used()
{
}

You must declare constructor and destructor in your class:

class used
{
public:
    used();
    ~used();
    int member=0;
};

Then here:

my_queue.resize(6);

you will actually create 6 pointers that will be initialized to nullptr. Maybe you're aware of that, since you expect my_queue[5] to return 0.

Then in your loop, everytime you do this:

my_queue.push_back(object);

you will increase the size of my_queue by one, thus make your loop run forever.

Apart from that: Do. Not. Do. using namespace std;. Ever.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43