-2

I am new to c++ and this one has me stumped. I want to pass a struct to a class (I know they are technically the same) so the class can access the data in the struct. I don't mind if it is passed as a reference or a copy as there will be no changes to the struct within the class. Having said that a reference would probably be better for performance. I can get it all to work fine if I pass the members of the struct individually but the real version will have about 30 members so passing them individually isn't the best option.

My main cpp:

#include "stdafx.h"
#include "myClass.h"
#include <iostream>
using namespace std;

struct foo
{
    int num;
    double dbl;
};

int _tmain(int argc, _TCHAR* argv[])
{
    foo bar;
    bar.dbl=3.14;
    bar.num=42;
    baz qux();  //bar needs to be passed here
    cout<<qux.getSum()<<endl;
    return 0;
}

Class header:

using namespace std;

class baz
{
public:
    baz();  //This is where type of bar (foo) is declared
    void setSum(int, double);
    double getSum();
private:
    double sum;
};

Class cpp:

#include "stdafx.h"
#include "myClass.h"
#include <iostream>
using namespace std;

baz::baz()  //this is where bar is called
{
    setSum(bar.num, bar.dbl);
}

void baz::setSum(int num, double dbl)
{
    sum=num*dbl;
}

double baz::getSum()
{
    return sum;
}

So the nub of the question is, how do I get bar into baz?

Modred
  • 249
  • 3
  • 15
  • Uhm, not clear if you want pass `bar` to the constructor [if so, what do you want to do with it?] or pass the `double` inside `bar`... – Mats Petersson Feb 09 '13 at 13:23
  • 3
    Come back when you've read one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Lightness Races in Orbit Feb 09 '13 at 13:23
  • 3
    `baz qux();` doesn't do what you think it does. – Lightness Races in Orbit Feb 09 '13 at 13:24
  • there is no `_tmain` in C++. – Abyx Feb 09 '13 at 13:31
  • Yes, I want to pass bar to the constructor. Once there, in the real code, bar will be loaded from a file and have about 30 members which will be used for all sorts of things, this is only a simple abstract to get the idea of how to pass it. In reality, baz will have up to 4k instances within a 3d vector, each with about 40 variables. I didn't think it would help to post that much code when everything else is working. – Modred Feb 09 '13 at 13:33
  • @Abyx There isn't? But it compiles with a C++ compiler, right? Does this really matter, here?? – πάντα ῥεῖ Feb 09 '13 at 13:35
  • 1
    @Abyx _tmain() is not part of the standard but that doesn't mean there's a problem with it. For hyperbolic example, the OP may have his own proper `main` function that isn't included here. In all likelyhood though, he's compiling with Visual Studio (which has its own standards, one of which allows for `_tmain` as the OP is using). – mah Feb 09 '13 at 13:36
  • @LRIO, I have books, although maybe not the best on the market, but I couldn't find what I needed in them hence coming here with an abstract of what I'm doing. – Modred Feb 09 '13 at 14:02

3 Answers3

3

Solved, put the struct definition into a separate header and included that where ever it was needed. Then just a simple change to the declaration in the class header and everything works perfectly. I have included the new code (minus the VS code for the benefit of Abyx who seems to find it so offensive) in case anyone else has the same problem.

Main cpp:

#include "myClass.h"
#include "foo.h"
#include <iostream>
using namespace std;

int main(int argc, _TCHAR* argv[])
{
    foo bar;
    bar.dbl=3.14;
    bar.num=42;
    baz qux(bar);
    cout<<qux.getSum()<<endl;
    return 0;
}

foo.h:

struct foo
{
    int num;
    double dbl;
};

myClass.h:

class baz
{
public:
    baz(const struct foo&);
    void setSum(int, double);
    double getSum();
private:
    double sum;
};

myClass.cpp:

#include "myClass.h"
#include "foo.h"
#include <iostream>
using namespace std;

baz::baz(const foo& blah)
{
    setSum(blah.num, blah.dbl);
}

void baz::setSum(int num, double dbl)
{
    sum=num*dbl;
}

double baz::getSum()
{
    return sum;
}
Modred
  • 249
  • 3
  • 15
0

Make additional constructor. Something like that:

    baz::baz(const foo& st){
      setSum(st);
    }

    void baz::setSum(const foo& st) 
    {
      setSum(st.num, st.dbl);
    }

then you can:

    int _tmain(int argc, _TCHAR* argv[])
    {
        foo bar;
        bar.dbl=3.14;
        bar.num=42;
        baz qux(bar);  //bar needs to be passed here
        cout<<qux.getSum()<<endl;
        return 0;
    }
K. Bulatov
  • 144
  • 3
0

Your main program:

#include "stdafx.h"
#include "myClass.h"
#include <iostream>
using namespace std;

struct foo
{
    int num;
    double dbl;
};

int _tmain(int argc, _TCHAR* argv[])
{
    foo bar;
    bar.dbl=3.14;
    bar.num=42;
    baz qux();  //bar needs to be passed here
    cout<<qux.getSum()<<endl;
    return 0;
}

Well, stdafx.h is a header that only makes sense if you turn on (or omit to turn off) use of Microsoft precompiled headers, which makes the Visual C++ preprocessor behave in decidedly non-standard ways. So better remove that. Also, _tmain is a Windows 9x support macro, that expands to either a standard main or a Microsoft-specific wmain. So ditch that also. Finally, there's no need for the return 0 at the end of a standard main, because that's the default return value of main. Also, what's that getSum? Do you write e.g. getsin or getcos? Better call it just sum. Then,

#include "myClass.h"
#include <iostream>
using namespace std;

struct foo
{
    int num;
    double dbl;
};

int main()
{
    foo bar;
    bar.dbl=3.14;
    bar.num=42;
    baz qux( bar );  // See how easy it is to pass `bar`
    cout<<qux.sum()<<endl;
}

For the baz class header file, DO NOT EVER have using namespace std in the global namespace in a header file. I.e. remove that also. Then,

#pragma once
#include -- relevant header --

class baz
{
private:
    double sum_;
public:
    baz( foo const& blah ): sum_( blah.whatever + something ) {}
    void setSum( int const n, double const v ) { sum_ = n*v; }
    double sum() const { return sum_; }
};

Note: #pragma once is a de facto standard, but it's not ISO standard.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Do heed LRIO's advice to [get yourself a C++ textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Cheers and hth. - Alf Feb 09 '13 at 13:34
  • As much as I would like to remove the MS stuff, that's what I'm using to compile and VS will not let you do that without it, especially the header. I have tried something like this before but the problem comes when we get to the class. If I declare in the header baz(foo const&) then baz::baz(foo const& blah) in the cpp I get a mass of errors. If I try the other way round, baz(const foo&) and baz::baz(const foo& blah) I only fail with foo being undefined. I am sure it's a syntax error somewhere. – Modred Feb 09 '13 at 14:33
  • @Modred: simply turn off use of precompiled headers in the Visual Studio project settings. – Cheers and hth. - Alf Feb 09 '13 at 21:53