In my Function.h file:
class Function{
public:
Function();
int help();
};
In my Function.cpp file:
#include "Function.h"
int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}
In my Main.cpp
#include <iostream>
#include "Function.h"
using namespace std;
int menu(){
Function fc;
fc.help();
return 1;
}
int main(int args, char**argv){
return menu();
}
Error is : ‘Function’ has not been declared
Can anybody tell me why? Thank you.
I tried like this and the problem is solved, but I dont really understand why:
In Function.h file:
I use
class Function{
public:
int status;
Function():status(1){}
int help();
};
instead of the old one
class Function{
public:
Function();
int help();
};
jefflunt
33.4k7 gold badges88 silver badges126 bronze badges
asked Mar 11, 2011 at 5:25
2
All your include statements are missing the #
:
#include "Function.h"
^
Everything else looks fine, though you need to also #include <iostream>
in Function.cpp since you’re using cout
.
Here is the Function.cpp that I got to compile and run:
#include "Function.h"
#include <iostream>
int Function::help() // No error here
{
using namespace std;
cout << "Help";
return 1;
}
Function::Function()
{
}
answered Mar 11, 2011 at 5:27
CameronCameron
95.5k25 gold badges194 silver badges222 bronze badges
4
I had a similar problem. Make sure that you only have the required header files. I had two header files both including each other and it spit out this mistake.
answered Dec 1, 2012 at 1:59
DanielDaniel
611 silver badge1 bronze badge
You have created a declaration for the constructor of the Function class without including it in your implementation (cpp file).
#include "Function.h"
Function::Function(){
// construction stuff here
}
int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}
answered Mar 11, 2011 at 5:28
GWWGWW
43k11 gold badges113 silver badges108 bronze badges
2
In the first Function.h file you have declared the constructor but not defined it. In the second Function.h file (the one that works) you have defined and declared the Function constructor. You can either define and declare in the header or file, or declare in the header file and define in the Function.cpp file.
For example, declare in the header file «Function.h»:
class Function
{
Function();
}
and define here in «Function.cpp»:
Function::Function(){}
Or the alternative is to declare and define in the header file «Function.h»:
Class Function
{
Function(){}
}
The other thing that you have done in the second version of the header file is to initialise the member variable «status» in the «member initialisation list» which is a good thing to do (See Effective C++ by Scott Meyers, Item 4). Hope this helps
answered May 19, 2012 at 16:50
Jake_HowardJake_Howard
2,5032 gold badges15 silver badges13 bronze badges
I have experienced a similar problem and it took me a while to find out why.
In your case, you may define PROBLEMCLASS_H in some other header files.
The consequence is your cpp file will skip the definition in the header file. In other words, the line #include "problemclass.h"
is skipped.
In my case, I am using MingW64 under Linux. Say I have a header file IO.h:
// IO.h
#ifndef _IO_H_
#define _IO_H_
class A{
...
};
#endif
In my main.cpp file:
// main.cpp
#include <unistd.h>
#include "IO.h"
int main(int argc, char** argv) {
//...
}
The cpp file looks innocent. However, when unistd.h
is included, it secretly includes /usr/i686-w64-mingw32.static/include/io.h
supplied by MingW, and this io.h
looks like:
// io.h
#ifndef _IO_H_
#define _IO_H_
...
#endif /* End _IO_H_ */
Now you can see that inclusion of unistd.h
will lead to the inclusion io.h
from MingW, and that will hide my own IO.h. I guess that’s a similar problem like yours.
If you switch the order of includes (put #include <unistd.h>
after IO.h), the program compiles. But this is not a good suggestion. I recommend that you don’t use _IO_H_ to guard your own IO.h.
To understand how/why your PROBLEMCLASS_H
is included, I agree with @greatwolf, you can use g++ -E
to output the preprocessor output and manually examine it. Check what files are included before your PROBLEMCLASS_H
and in what order they are included. I hope that can help solve your problem.
Loading
- Forum
- General C++ Programming
- Struct has not beed declared
Struct has not beed declared
Hey guys.
I’m currently having trouble accessing a struct from a templated base class
|
|
However this always gives me the error
error: ‘Dummy_Test’ has not been declared
I know i can fix this simply be writing
void Foo(typename CBase<size>::Dummy_Test& test);
However names could get pretty long. I was trying to shorten this be deriving from the base class.
Could you help me out here?
Last edited on
But then why not just declare the Dummy_Test struct outside of your CBase class template?
Since it appears your Dummy_Test struct does not depend on the template size parameter of CBase.
Fundamentally, part of the issue is that
CBase<2>::Dummy_Test and
CBase<3>::Dummy_Test
are different types, as far as the compiler cares.
|
|
Edit:
For what it’s worth, you can also do this:
|
|
Last edited on
Hey this was my error in this example. Of course std::array must depend on the size type
So
|
|
would be correct.
Your using solution works nicely.
How far is the scope of this using directive
|
|
;
It does not seem to be applied to CDerived2 because Dummy_Test<2> is not known.
So can i say that using xxx only applies to the next class or function and is not globally available?
Thx for your fast help
@JuliusCaeser
Several minor issues get in your way.
First, your example using declaration used Dummy_test, the the «T» is capitalized, Dummy_Test
Second, the struct is private (CBase is a class, the struct is declared in the default private section). Make it public.
You’re missing a «;» after the CDerived2 class.
Then, it passes compilation.
ah thx very much that did help
I sometimes find it hard to spot my own mistakes
Topic archived. No new replies allowed.
Имеется программа, во время компиляции на Dev-C++ выпадает ошибка «`Rectangle’ 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<<«nntt«;
system(«PAUSE»);
return EXIT_SUCCESS;
}
Файл 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;
};
Файл Rect.cpp
Код:
#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);
}
Если удалить из проекта файл Rect.cpp, но оставить строку «#include «Rect.cpp» » все компилируется и работает. Объясните нубу в чем ошибка.
3 ответа
У тебя неподрублен Rect.hpp в Rect.cpp от того компилятор и пишет что
Rectangle
неизвестен в нем
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
ну и указываем что
Rectangle
и
GetArea
находится в другом файле и их ненадо заного создавать
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);
}
проверил только на компиляцию в VS2010
85K
08 октября 2012 года
Lakroft
6 / / 04.10.2012
Не понятно, почему в файл Rect.hpp нужно подрубать этот-же Rect.hpp. У меня заработало после пары изменений:
- В файле 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
- В файле main
Удалил строку #include «Rect.cpp»
Код:
#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<<«nntt«;
system(«PAUSE»);
return EXIT_SUCCESS;
}
Не понятно, почему в файл Rect.hpp нужно подрубать этот-же Rect.hpp.
Код:
#ifndef Rect_hpp //проверка подрублен ли Rect.hpp (вместо точки нижнее подчёркивание)
#define Rect_hpp //если не подрублен то подрубить
//Rect.hpp
#endif //конец для #define
тоже самое что
Код:
if(«Rect.hpp» подключен?) ничего неделать
else подключить Rect.hpp
нужно для того чтобы дважды неподключать Rect.hpp (в Rect.cpp и Main.cpp) если этого не сделать то он будет рекурсивно подключаться и не скомпилируется…
а подключать нужно в каждом файле.