objects as constructors

hi I'm wondering what I'm doing wrong I have passed an object of class to another class that accepts an objext as that class as it's parameter,I'm lost as to this won't work for me,

Thanks here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #ifndef ONE_H
#include <iostream>
#define ONE_H
#include <iostream>
using namespace std;

one.h
class one
{
    public:
        one(int x);

    protected:

    private:
};

#endif // ONE_H 


one.cpp
1
2
3
4
5
6
7
8
#include "one.h"
#include <iostream>
using namespace std;
one::one(int x)
{
    //ctor
}


main
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "one.h"

using namespace std;

int main()
{
    one ou(3);
    two oa(ou);

}


two.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef TWO_H
#define TWO_H
#include "one.h"
#include <iostream>
using namespace std;
class two
{
    public:
        two(one oo);

    protected:

    private:
};

#endif // TWO_H



1
2
3
4
5
6
7
8
9

#include "two.h"
#include "one.h"
#include <iostream>
using namespace std;
two::two(one oo)
{
    //ctor
}


I'm lost as to this won't work for me,

You're going to have to more specific. Are you getting compile errors? If so, what are the errors?

main.cpp line 9: Uses two, but two.h is not included.
I forgot to also use a member intilisation list but that did not solve the problem

here are the errors I'm getting

line 6 candidates are two::two(const two&)
line 9 error: two::two(one)

and also

line 5 error: prototype for 'two::two()' does not match any in class 'two'
Last edited on
main.cpp line 9: You're passing ou by value. This requires that one have a copy constructor.
thanks for the reply,

what do you mean by a copy constructor?

Thanks
The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to: Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.
http://www.tutorialspoint.com/cplusplus/cpp_copy_constructor.htm
Topic archived. No new replies allowed.