asdasd123123
شنبه 03 اسفند 1392, 11:59 صبح
سلام دوستان
می خوام فقط help.o (آبجکت شده ی help.cpp ) و help.h در اختیار
سرویس گیرنده باشه. (هدف اصلی اینه که value مخفی باشه.)
ولی کد زیر ارور میده. چه کنم؟
//main.cpp
#include "help.h"
#include <iostream>
using namespace std;
int main()
{
proxy x(5); //no error
cout<<(x.var)->get_value(); //error
/*
error:
...\try\main.cpp|In function 'int main()':|
...\try\main.cpp|Line 8|error: invalid use of incomplete type 'class myType'|
...\try\help.h|Line 4|error: forward declaration of 'class myType'|
|=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|
*/
}
.
//help.h
#ifndef __help__H
#define __help__H
class myType;
class proxy
{
public:
proxy(const int&);
~proxy();
myType* var;
};
#endif
.
//help.cpp
#include "help.h"
#include "base.h"
proxy::proxy(const int& _value): var(new myType(_value))
{
}
proxy::~proxy()
{
delete var;
}
.
//base.h
#ifndef __base__H
#define __base__H
class myType
{
public:
myType(const int&);
~myType();
const int& get_value();
private:
int value;
};
myType::myType(const int& _value): value(_value)
{
}
myType::~myType()
{
}
const int& myType::get_value()
{
return value;
}
#endif
می خوام فقط help.o (آبجکت شده ی help.cpp ) و help.h در اختیار
سرویس گیرنده باشه. (هدف اصلی اینه که value مخفی باشه.)
ولی کد زیر ارور میده. چه کنم؟
//main.cpp
#include "help.h"
#include <iostream>
using namespace std;
int main()
{
proxy x(5); //no error
cout<<(x.var)->get_value(); //error
/*
error:
...\try\main.cpp|In function 'int main()':|
...\try\main.cpp|Line 8|error: invalid use of incomplete type 'class myType'|
...\try\help.h|Line 4|error: forward declaration of 'class myType'|
|=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|
*/
}
.
//help.h
#ifndef __help__H
#define __help__H
class myType;
class proxy
{
public:
proxy(const int&);
~proxy();
myType* var;
};
#endif
.
//help.cpp
#include "help.h"
#include "base.h"
proxy::proxy(const int& _value): var(new myType(_value))
{
}
proxy::~proxy()
{
delete var;
}
.
//base.h
#ifndef __base__H
#define __base__H
class myType
{
public:
myType(const int&);
~myType();
const int& get_value();
private:
int value;
};
myType::myType(const int& _value): value(_value)
{
}
myType::~myType()
{
}
const int& myType::get_value()
{
return value;
}
#endif