header file - containing......header files

Started by
12 comments, last by DrunkenHyena 19 years, 2 months ago
I have a problem :/ I have a header file that consists of every header file in my project. #ifndef HEADER_LIST_H #define HEADER_LIST_H #include <Windows.h> #include <mmsystem.h> #include <d3dx9.h> #include <d3d9.h> #include "globals.h" #include "render.h" #include "graphics.h" #endif in each header file, I #include "header list.h". In render.h, I have a function "Render()" that calls another function "SetupMatrices". "SetupMatrices()" is located in graphics.h. I get these two errors when I try to compile: render.h(24) : error C3861: 'SetupMatrices': identifier not found, even with argument-dependent lookup graphics.h(48) : error C2365: 'SetupMatrices' : redefinition; previous definition was a 'formerly unknown identifier' here is my render header file and graphics header file: #ifndef RENDER_H #define RENDER_H #include "header list.h" //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { if( NULL == g_pd3dDevice ) return; // Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Setup the world, view, and projection matrices SetupMatrices(); // Render the vertex buffer contents g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 ); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } #endif --------------------------------------------------------------------------- #ifndef GRAPHICS_H #define GRAPHICS_H #include "header list.h" //----------------------------------------------------------------------------- // Name: InitGeometry() // Desc: Creates the scene geometry //----------------------------------------------------------------------------- HRESULT InitGeometry() { // Initialize three vertices for rendering a triangle CUSTOMVERTEX g_Vertices[] = { { -1.0f,-1.0f, 0.0f, 0xffff0000, }, { 1.0f,-1.0f, 0.0f, 0xff0000ff, }, { 0.0f, 1.0f, 0.0f, 0xffffffff, }, }; // Create the vertex buffer. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) { return E_FAIL; } // Fill the vertex buffer. VOID* pVertices; if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, g_Vertices, sizeof(g_Vertices) ); g_pVB->Unlock(); return S_OK; } //----------------------------------------------------------------------------- // Name: SetupMatrices() // Desc: Sets up the world, view, and projection transform matrices. //----------------------------------------------------------------------------- VOID SetupMatrices() { // For our world matrix, we will just rotate the object about the y-axis. D3DXMATRIXA16 matWorld; // Set up the rotation matrix to generate 1 full rotation (2*PI radians) // every 1000 ms. To avoid the loss of precision inherent in very high // floating point numbers, the system time is modulated by the rotation // period before conversion to a radian angle. UINT iTime = timeGetTime() % 1000; FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f; D3DXMatrixRotationY( &matWorld, fAngle ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); // Set up our view matrix. A view matrix can be defined given an eye point, // a point to lookat, and a direction for which way is up. Here, we set the // eye five units back along the z-axis and up three units, look at the // origin, and define "up" to be in the y-direction. D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f ); D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f ); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); // For the projection matrix, we set up a perspective transform (which // transforms geometry from 3D view space to 2D viewport space, with // a perspective divide making objects smaller in the distance). To build // a perpsective transform, we need the field of view (1/4 pi is common), // the aspect ratio, and the near and far clipping planes (which define at // what distances geometry should be no longer be rendered). D3DXMATRIXA16 matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); } #endif the only way I can fix the error is to #include "graphics.h" inside of "render.h".... I shouldn't have to though because I already included "header list.h" inside of "render.h" if you see something I don't, please reply
Advertisement
your putting the function bodys in header files?


I didn't ask for commentary about my organization, I asked if anyone could help me figure out why I was getting the errors.
The reason why I put the functions' bodies inside of the .h files is because when I put them into .cpp files, I would get errors saying that the functions were undeclared when I try to call them in my winmain function
this is whats happening
// h.h#ifndef H_H#define H_H#include "a.h"#include "b.h"#endif// a.h#ifndef A_H#define A_H#include "h.h"void callb(){b();}void a(){};#endif// b.h#ifndef B_H#define B_H#include "h.h"void callb(){a();}void b(){};#endif// m.cpp#include "h.h"int main(){	calla();	return 0;}

// step 1#include "h.h"int main(){	calla();	return 0;}// step 2#ifndef H_H#define H_H#include "a.h"#include "b.h"#endifint main(){	calla();	return 0;}// step 3#ifndef H_H#define H_H#ifndef A_H#define A_H#include "h.h"void callb(){b();}void a(){};#endif#ifndef B_H#define B_H#include "h.h"void callb(){a();}void b(){};#endif#endifint main(){	calla();	return 0;}// step 4#ifndef H_H#define H_H#ifndef A_H#define A_H#ifndef H_H#endifvoid callb(){b();}void a(){};#endif#ifndef B_H#define B_H#ifndef H_H#endifvoid callb(){a();}void b(){};#endif#endifint main(){	calla();	return 0;}// step 5#ifndef H_H#define H_H#ifndef A_H#define A_H#ifndef H_H#endifvoid callb(){b();} // ERROR HERE!!! undefinedvoid a(){};#endif#ifndef B_H#define B_H#ifndef H_H#endifvoid callb(){a();}void b(){};#endif#endifint main(){	calla();	return 0;}
Whether or not you want to hear about your organization, it is likely causing the problem. With the exception of class members, defining functions in headers is a very bad idea. The only reason you can (almost) get away with it now, if I understand your situation correctly, is that you only have one .cpp file. Declare the function in the header, then define it in a .cpp file. The errors you got probably result from either 1) not including the appropriate header in the file with WinMain() in it or 2) not including the .cpp file in your project. It is not enough to simply create it, you must add it to the list of project files, as well.

Learning to properly organize multi-file projects does take some effort, but the long term benefits in terms of avoided linker errors and what-not are tremendous. I can't be sure if your poor organization is what is causing your problem yet, but certainly no one wants to try to debug what is obviously some sort of file organization issue when the organization is so messy.


Ok. I put all of my function declarations into .h files and all of the function bodies into .cpp files. I'm still getting an error stating that the functions are not declared. I use one header file "header list.h" to contain every other .h file in it. I include "header list.h" in every .h and .cpp file.
All of my .cpp files are included into my project folder. What else could I be doing wrong?
Quote:I include "header list.h" in every .h and .cpp file.

including header list.h in all .h files may be the problem
try removing

#include "header list.h"

ummm...from which file? because every one of my .h files needs the header files located in "header list.h"
Quote:because every one of my .h files needs the header files located in "header list.h"

please reread

This topic is closed to new replies.

Advertisement