-2

I am trying to have a struct declared in my class and then return a pointer to that struct using a member function in that class, but it does not work.

Here is my code:

someClass.h:

class someClass
{
public:        
    someClass();
    sword* returnSword();        

    struct sword
    {
        int str;
        char name;
    };

    sword* m_sword;
};

someClass.cpp:

#include "someClass.h"

someClass::someClass()
{
    m_sword = new sword;

    m_sword->name='s';
    m_sword->str=5;
}


someClass::sword* someClass::returnSword()
{    
    return m_sword;    
}

In case if my code is total nonsense, please explain how can I have a struct in my class, initialize it and then return a pointer from that class pointing to that struct.

Thank you.

Barry
  • 286,269
  • 29
  • 621
  • 977
user1335175
  • 181
  • 7
  • 20
  • 7
    Define "doesn't work". What happens, and what do you expect to happen differently? I don't see anything obviously wrong in the code you've shown. – Igor Tandetnik May 17 '15 at 03:33
  • Well If I try to compile, it gives me bunch of errors, but even before, in the initialization of the function returnSword() function name is highlighted red with a following text. declaration is incompatible with " *someClass::returnSword()" – user1335175 May 17 '15 at 03:35
  • 3
    Move the definition of `sword` above that of the declaration of `returnSword`. It seems very unnecessary to dynamically allocate a `sword`, and if you do that then your class needs to follow the [Rule of Three](https://stackoverflow.com/q/4172722/241631). – Praetorian May 17 '15 at 03:37
  • I don't have much experience with pointers of this type, so when I look at the code, I feel like everything is correct, and I expect returnSword to return a pointer to the struct, but it does not work, many many errors. – user1335175 May 17 '15 at 03:38
  • Where and how do you define the sword type? – Alan Wolfe May 17 '15 at 03:39
  • Praetorian Thank you, you are right, I had to define sword in advance, and yes dynamic allocation was actually unnecessary. – user1335175 May 17 '15 at 03:40
  • One more question. How can I do this and define my sword struct to be a private variable ? – user1335175 May 17 '15 at 03:41

3 Answers3

4

Always post the compile errors you get. In this case:

error: 'sword' does not name a type
  sword* returnSword();
  ^

OK. Why doesn't sword name a type? Because it hasn't been declared yet. You have to declare sword before you make any references to it:

class someClass
{
public:        
    struct sword   // <== must be declared
    {
        int str;
        char name;
    };

    someClass();
    sword* returnSword();   // <== before you use it
    // ...
};

Although do you really need m_sword to be a pointer? Could just make it a value and have returnSword() return a reference. That way you can't forget to delete it in the destructor which you didn't write...

Barry
  • 286,269
  • 29
  • 621
  • 977
1

The line

sword* returnSword();

is a problem. sword is not known at that line. Move the definition of sword before that line or provide a forward declaration.

class someClass
{
public:        
    someClass();

    struct sword
    {
        int str;
        char name;
    };

    sword* returnSword();        


    sword* m_sword;
};

or

class someClass
{
public:        
    someClass();

    struct sword;

    sword* returnSword();        

    struct sword
    {
        int str;
        char name;
    };

    sword* m_sword;
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Using forward declaration of sword is another way to solve the problem.

class someClass
{
public:        
    someClass();
    struct sword; // FORWARD declaration for sword
    sword* returnSword();        

    struct sword
    {
        int str;
        char name;
    };

    sword* m_sword;
};
Nipun Talukdar
  • 4,975
  • 6
  • 30
  • 42