0

I'm relatively new in this forum and to object oriented language so please forgive me if this seems kinda stupid. I just want to test calling a function, specifically a constructor, with arrays as arguments.

main source

#include <iostream>
#include "d.h"

using namespace std;

int main () {

double pos[2] =    { 2, 3} ;
double speed[2] =  { 0, 0} ;
double accel[2] =  { 1, 0} ;

Body human(pos, speed, accel);
} 

class header

#ifndef D_H
#define D_H


class Body {

public:
    Body(double k[], double l[], double m[]);
    ~Body();
protected:
    double p[2] ;
    double v[2] ;
    double a[2] ;
private:

};

#endif // D_H

class source

#include "d.h"
#include <iostream>

Body::Body (double k[], double l[], double m[])
:   p(k) ,
    v(l) ,
    a(m)
{
//ctor
}

Body::~Body(){
}

I am getting the Build Messages:

undefined reference to 'Body::Body(double*, double*, double*)'
undefined reference to 'Body::~Body()'

Would be nice if someone could help me. How do I correctly use the arrays in my constructor? And why does the same problem apply to my deconstructor? It says the same when i completely delete the deconstructor (declaration AND implementation).

Christoph
  • 4,251
  • 3
  • 24
  • 38
AyJ3y
  • 1
  • 1
  • 2
    Are you building the source file containing the definition of the constructor and the destructor as part of the project, and linking the result into the executable? – Andy Prowl May 29 '15 at 22:57
  • 1
    I would second Andy. Looks like you are not building the source file for `Body` – The Vivandiere May 29 '15 at 22:59
  • Okay I didn't build the source file. I am using Code::Blocks and thought it would build them automatically when I tell it to unclude the header. – AyJ3y May 30 '15 at 12:20

2 Answers2

0

You are probably not linking to the class source file. You can do so by simply providing both source files on the command line.

You've got bigger problems than that though. Your constructor is taking in by pointer and trying to assign to arrays. This won't work. As soon as you add the class source to the compile command you should get errors.

I would recommend using std::array for all places you are using arrays. So for example your class declaration would change to this:

struct Body {
    Body(std::array<double,2> const& k, std::array<double,2> const& l, std::array<double,2> const& m);

private:
    std::array<double,2> p;
    std::array<double,2> v;
    std::array<double,2> a;
};

Doing that, your use of initializer list to initialize the array will function correctly. It will copy all elements, which given this construct would seem to be what you want.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
0

You can write the whole thing in one file. Next, put it in different files like you had before, but make sure both .cpp files are compiled and linked. If class definition for Body is not compiled and linked then the compiler doesn't know it's there.

#include <iostream>

//class declaration
class Body 
{
    double pos[2];
    double speed[2];
    double accel[2];
public:
    Body(const double p[2], const double s[2], const double a[2]);
};

//class definition 
Body::Body(const double p[2], const double s[2], const double a[2])
{ 
    std::copy(p, p + 2, pos); 
    std::copy(s, s + 2, speed); 
    std::copy(a, a + 2, accel); 
    for (auto item : pos) 
        std::cout << item << std::endl; 
}

int main()
{
    double pos[2] = { 2, 3 };
    double speed[2] = { 0, 0 };
    double accel[2] = { 1, 0 };
    Body human(pos, speed, accel);
}

For full c++ standard you can follow the previous answer with std::array or better yet std::vector.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77