Почему компилятор выдает ошибку, что объявленный класс "has not been declared "?
Код:
#include <cstdlib>
#include <iostream>
using namespace std;
#include "Rect.hpp"
#include "Rect.cpp"
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "Russian");
Rectangle MyRectangle (100, 20, 50, 80); //
int Area = MyRectangle.GetArea();
cout << "Area = "<<Area<<endl;
cout <<"Upper Left X coordinate: ";
cout <<MyRectangle.GetUpperLeft().GetX();
cout<<"\n\n\t\t";
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
using namespace std;
#include "Rect.hpp"
#include "Rect.cpp"
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "Russian");
Rectangle MyRectangle (100, 20, 50, 80); //
int Area = MyRectangle.GetArea();
cout << "Area = "<<Area<<endl;
cout <<"Upper Left X coordinate: ";
cout <<MyRectangle.GetUpperLeft().GetX();
cout<<"\n\n\t\t";
system("PAUSE");
return EXIT_SUCCESS;
}
Код:
#include <cstdlib>
#include <iostream>
using namespace std;
class Point
{
public:
void SetX(int x) {itsX = x;}
void SetY(int y) {itsY = y;}
int GetX() const {return itsX;}
int GetY() const {return itsY;}
private:
int itsX;
int itsY;
};
class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle() {}
int GetTop() const {return itsTop;}
int GetLeft() const {return itsLeft;}
int GetBottom() const {return itsBottom;}
int GetRight() const {return itsRight;}
Point GetUpperLeft() const {return itsUpperLeft;}
Point GetLowerLeft() const {return itsLowerLeft;}
Point GetUpperRight() const {return itsUpperRight;}
Point GetLowerRight() const {return itsLowerRight;}
void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
void SetUpperRight(Point Location) {itsUpperRight = Location;}
void SetLowerRight(Point Location) {itsLowerRight = Location;}
void SetTop(int top) {itsTop = top;}
void SetLeft(int left) {itsLeft = left;}
void SetBottom(int bottom) {itsBottom = bottom;}
void SetRight(int right) {itsRight = right;}
int GetArea() const;
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
#include <iostream>
using namespace std;
class Point
{
public:
void SetX(int x) {itsX = x;}
void SetY(int y) {itsY = y;}
int GetX() const {return itsX;}
int GetY() const {return itsY;}
private:
int itsX;
int itsY;
};
class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle() {}
int GetTop() const {return itsTop;}
int GetLeft() const {return itsLeft;}
int GetBottom() const {return itsBottom;}
int GetRight() const {return itsRight;}
Point GetUpperLeft() const {return itsUpperLeft;}
Point GetLowerLeft() const {return itsLowerLeft;}
Point GetUpperRight() const {return itsUpperRight;}
Point GetLowerRight() const {return itsLowerRight;}
void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
void SetUpperRight(Point Location) {itsUpperRight = Location;}
void SetLowerRight(Point Location) {itsLowerRight = Location;}
void SetTop(int top) {itsTop = top;}
void SetLeft(int left) {itsLeft = left;}
void SetBottom(int bottom) {itsBottom = bottom;}
void SetRight(int right) {itsRight = right;}
int GetArea() const;
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
Код:
#include <cstdlib>
#include <iostream>
using namespace std;
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);
itsUpperRight.SetX(right);
itsUpperRight.SetY(top);
itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);
itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
}
int Rectangle::GetArea() const
{
int Width = itsRight - itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}
#include <iostream>
using namespace std;
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);
itsUpperRight.SetX(right);
itsUpperRight.SetY(top);
itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);
itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
}
int Rectangle::GetArea() const
{
int Width = itsRight - itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}
Rect.hpp
Код:
#ifndef Rect_hpp //проверка подрублен ли Rect.hpp (вместо точки нижнее подчёркивание)
#define Rect_hpp //если не подрублен то подрубить
#include <cstdlib>
#include <iostream>
using namespace std;
class Point
{
public:
void SetX(int x) {itsX = x;}
void SetY(int y) {itsY = y;}
int GetX() const {return itsX;}
int GetY() const {return itsY;}
private:
int itsX;
int itsY;
};
class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle() {}
int GetTop() const {return itsTop;}
int GetLeft() const {return itsLeft;}
int GetBottom() const {return itsBottom;}
int GetRight() const {return itsRight;}
Point GetUpperLeft() const {return itsUpperLeft;}
Point GetLowerLeft() const {return itsLowerLeft;}
Point GetUpperRight() const {return itsUpperRight;}
Point GetLowerRight() const {return itsLowerRight;}
void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
void SetUpperRight(Point Location) {itsUpperRight = Location;}
void SetLowerRight(Point Location) {itsLowerRight = Location;}
void SetTop(int top) {itsTop = top;}
void SetLeft(int left) {itsLeft = left;}
void SetBottom(int bottom) {itsBottom = bottom;}
void SetRight(int right) {itsRight = right;}
int GetArea() const;
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
#endif //конец для #define
#define Rect_hpp //если не подрублен то подрубить
#include <cstdlib>
#include <iostream>
using namespace std;
class Point
{
public:
void SetX(int x) {itsX = x;}
void SetY(int y) {itsY = y;}
int GetX() const {return itsX;}
int GetY() const {return itsY;}
private:
int itsX;
int itsY;
};
class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle() {}
int GetTop() const {return itsTop;}
int GetLeft() const {return itsLeft;}
int GetBottom() const {return itsBottom;}
int GetRight() const {return itsRight;}
Point GetUpperLeft() const {return itsUpperLeft;}
Point GetLowerLeft() const {return itsLowerLeft;}
Point GetUpperRight() const {return itsUpperRight;}
Point GetLowerRight() const {return itsLowerRight;}
void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
void SetUpperRight(Point Location) {itsUpperRight = Location;}
void SetLowerRight(Point Location) {itsLowerRight = Location;}
void SetTop(int top) {itsTop = top;}
void SetLeft(int left) {itsLeft = left;}
void SetBottom(int bottom) {itsBottom = bottom;}
void SetRight(int right) {itsRight = right;}
int GetArea() const;
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
#endif //конец для #define
Rect.cpp
Код:
#include <cstdlib>
#include <iostream>
#include "Rect.hpp"
using namespace std;
extern Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);
itsUpperRight.SetX(right);
itsUpperRight.SetY(top);
itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);
itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
}
extern int Rectangle::GetArea() const
{
int Width = itsRight - itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}
#include <iostream>
#include "Rect.hpp"
using namespace std;
extern Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);
itsUpperRight.SetX(right);
itsUpperRight.SetY(top);
itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);
itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
}
extern int Rectangle::GetArea() const
{
int Width = itsRight - itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}
проверил только на компиляцию в VS2010
- В файле Rect.hpp:
#ifndef Rect_hpp
#define Rect_hpp
на
#ifndef Rect_сpp
#define Rect_сpp
Код:
#ifndef Rect_cpp //проверка подрублен ли Rect.cpp
#define Rect_cpp //если не подрублен то подрубить
#include <cstdlib>
#include <iostream>
using namespace std;
class Point
{
public:
void SetX(int x) {itsX = x;}
void SetY(int y) {itsY = y;}
int GetX() const {return itsX;}
int GetY() const {return itsY;}
private:
int itsX;
int itsY;
};
class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle() {}
int GetTop() const {return itsTop;}
int GetLeft() const {return itsLeft;}
int GetBottom() const {return itsBottom;}
int GetRight() const {return itsRight;}
Point GetUpperLeft() const {return itsUpperLeft;}
Point GetLowerLeft() const {return itsLowerLeft;}
Point GetUpperRight() const {return itsUpperRight;}
Point GetLowerRight() const {return itsLowerRight;}
void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
void SetUpperRight(Point Location) {itsUpperRight = Location;}
void SetLowerRight(Point Location) {itsLowerRight = Location;}
void SetTop(int top) {itsTop = top;}
void SetLeft(int left) {itsLeft = left;}
void SetBottom(int bottom) {itsBottom = bottom;}
void SetRight(int right) {itsRight = right;}
int GetArea() const;
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
#endif //конец для #define
#define Rect_cpp //если не подрублен то подрубить
#include <cstdlib>
#include <iostream>
using namespace std;
class Point
{
public:
void SetX(int x) {itsX = x;}
void SetY(int y) {itsY = y;}
int GetX() const {return itsX;}
int GetY() const {return itsY;}
private:
int itsX;
int itsY;
};
class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle() {}
int GetTop() const {return itsTop;}
int GetLeft() const {return itsLeft;}
int GetBottom() const {return itsBottom;}
int GetRight() const {return itsRight;}
Point GetUpperLeft() const {return itsUpperLeft;}
Point GetLowerLeft() const {return itsLowerLeft;}
Point GetUpperRight() const {return itsUpperRight;}
Point GetLowerRight() const {return itsLowerRight;}
void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
void SetUpperRight(Point Location) {itsUpperRight = Location;}
void SetLowerRight(Point Location) {itsLowerRight = Location;}
void SetTop(int top) {itsTop = top;}
void SetLeft(int left) {itsLeft = left;}
void SetBottom(int bottom) {itsBottom = bottom;}
void SetRight(int right) {itsRight = right;}
int GetArea() const;
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
#endif //конец для #define
- В файле main
Код:
#include <cstdlib>
#include <iostream>
using namespace std;
#include "Rect.hpp"
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "Russian");
Rectangle MyRectangle (100, 20, 50, 80); //
int Area = MyRectangle.GetArea();
cout << "Area = "<<Area<<endl;
cout <<"Upper Left X coordinate: ";
cout <<MyRectangle.GetUpperLeft().GetX();
cout<<"\n\n\t\t";
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
using namespace std;
#include "Rect.hpp"
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "Russian");
Rectangle MyRectangle (100, 20, 50, 80); //
int Area = MyRectangle.GetArea();
cout << "Area = "<<Area<<endl;
cout <<"Upper Left X coordinate: ";
cout <<MyRectangle.GetUpperLeft().GetX();
cout<<"\n\n\t\t";
system("PAUSE");
return EXIT_SUCCESS;
}
Цитата: Lakroft
Не понятно, почему в файл Rect.hpp нужно подрубать этот-же Rect.hpp.
Код:
#ifndef Rect_hpp //проверка подрублен ли Rect.hpp (вместо точки нижнее подчёркивание)
#define Rect_hpp //если не подрублен то подрубить
//Rect.hpp
#endif //конец для #define
#define Rect_hpp //если не подрублен то подрубить
//Rect.hpp
#endif //конец для #define
Код:
if("Rect.hpp" подключен?) ничего неделать
else подключить Rect.hpp
else подключить Rect.hpp
а подключать нужно в каждом файле.