Header doesn't see prototypes...

This is a discussion on Header doesn't see prototypes... within the C++ Programming forums, part of the General Programming Boards category; My header file doesn't see the file where I have all my prototypes and I don't know why...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    Header doesn't see prototypes...

    My header file doesn't see the file where I have all my prototypes and I don't know why

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Your header file should contain the prototypes.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    >.< I am meaning the coding of the function

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Give more details. A file usually only needs to see the declarations of functions it uses, but headers don't usually use functions, source files do. One exception is class declarations (for base classes or class members).

    A file never has to see the definition of another function or class that it uses (except for templated code). Instead, you compile the definitions separately in separate source files and link them together at the end. Make sure all your cpp files are added to your project or makefile.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by Daved
    Give more details. A file usually only needs to see the declarations of functions it uses, but headers don't usually use functions, source files do. One exception is class declarations (for base classes or class members).

    A file never has to see the definition of another function or class that it uses (except for templated code). Instead, you compile the definitions separately in separate source files and link them together at the end. Make sure all your cpp files are added to your project or makefile.
    Like a .lib? Compile it how?


    HEADER
    Code:
    #include <iostream>
    #include <fstream>
    void DisplayOptions();
    void Overwrite(int Loops);
    void Delete(std::string File);
    int GetDiskSize(std::string Dir);
    int GetFileSize(std::string File);
    void CheckLength(std::string Dir);
    std::string GetObject(std::string Object);
    void MainFunction(std::string Dir,std::string File);
    void RestartMainFunction(std::string Dir,std::string File);
    void Choice(std::string Dir,bool Restart,std::string File);
    void NoRestartMainFunction(std::string Dir,std::string File);
    FUNCTIONS
    Code:
    Code in seperate source file
    #include "..\Header\SecSuite.h"
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    /*____________________________________________________________________________*/
    
    void DisplayOptions()
    {
    	std::cout<<"Welcome to C.J.'s Security 'suite'!\n";
    	std::cout<<"Please enter what you want to do.\n"<<std::endl;
    	std::cout<<"To select Secure Disk Wipe, input SDW.\n";
    	std::cout<<"To select Secure Disk Cleanse, input SDC.\n";
    	std::cout<<"To select Secure File Deletion, input SFD.\n";
    	std::cout<<"To select ---this option is not yet available---.\n";
    	std::cout<<"To select ---this option is not yet available---."<<std::endl;
    	std::cout<<"Option:";
    }
    /*____________________________________________________________________________*/
    void Choice(std::string Dir,bool Restart, std::string File)
    {
    	if (Restart=true)
    	{
    		RestartMainFunction(Dir,File);
    	}
    	if(Restart==false)
    	{
    		NoRestartMainFunction(Dir,File);
    	}
    }
    /*____________________________________________________________________________*/
    void Delete(std::string File)
    {
    	File="Del "+File;
    	system(File.c_str());
    	std::cout<<"File erased."<<std::endl;
    }
    /*____________________________________________________________________________*/
    int GetDiskSize(std::string Dir)
    {
    	__int64 i64FreeBytes;
    	typedef BOOL (WINAPI *PGETDISKFREESPACEEX)(LPCSTR,
    	PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);
    	PGETDISKFREESPACEEX pGetDiskFreeSpaceEx;
    	pGetDiskFreeSpaceEx=(PGETDISKFREESPACEEX)GetProcAddress(GetModuleHandle
    	("kernel32.dll"),
    	"GetFreeDiskSpaceExA");
    
        GetDiskFreeSpaceExA(Dir.c_str(),
    					NULL,
    					NULL,
    					(PULARGE_INTEGER)&i64FreeBytes);
    	int SizeOfDisk=((i64FreeBytes/1024)/1024);
    	return SizeOfDisk;
    }
    /*____________________________________________________________________________*/
    int GetFileSize(std::string File)
    {
    	long end,begin;
    	int SizeOfFile;
    	std::ifstream a_File(File.c_str());
    	begin=a_File.tellg();
    	a_File.seekg(0,std::ios::end);
    	end=a_File.tellg();
    	a_File.close();
    	SizeOfFile=(end-begin);
    	return SizeOfFile;
    }
    /*____________________________________________________________________________*/
    std::string GetObject(std::string Option)
    {
    	std::string DirOrFileName;
    	std::cout<<"Please enter "<<Option;
    	getline(std::cin,DirOrFileName,'\n');
    	return DirOrFileName;
    }
    /*____________________________________________________________________________*/
    void CheckLength(std::string Disk)
    {
    	bool Loop;
    	while(Loop==true)
    	{
    		Disk="";
    		std::cout<<"Please try again:";
    		getline(std::cin,Disk,'\n');
    		if(Disk.length()==3)
    		{
    			Loop=false;
    		}
    	}
    	Loop=true;
    }
    /*____________________________________________________________________________*/
    void MainFunction(std::string Dir,std::string File)
    {
    	std::ofstream a_file(File.c_str());
    	int Loops=GetDiskSize(Dir);
    	while(Loops>0,Loops--)
    	{
    		a_file<<"1";
    	}
    	a_file.close();
    	std::cout<<"No space left on Drive"<<std::endl;
    }
    /*____________________________________________________________________________*/
    void RestartMainFunction(std::string Dir,std::string File)
    {
    	std::ofstream a_file(File.c_str(), std::ios::ate);
    	int Loops=GetDiskSize(Dir);
    	while(Loops>0,Loops--)
    	{
    		a_file<<"1";
    	}
    	a_file.close();
    	std::cout<<"No space left on Drive\n"<<std::endl;
    }
    /*____________________________________________________________________________*/
    void NoRestartMainFunction(std::string Dir,std::string File)
    {
    	std::ofstream a_file(File.c_str(), std::ios::trunc);
    	int Loops=GetDiskSize(Dir);
    	while(Loops>0,Loops--)
    	{
    		a_file<<"1";
    	}
    	a_file.close();
    	std::cout<<"No space left on Drive\n"<<std::endl;
    }
    /*____________________________________________________________________________*/
    MAIN
    Code:
    #include "..\header\SecSuite.h"
    
    int main()
    {
    	std::string Dir,File,Option,Restart;
    	DisplayOptions();
    	getline(std::cin,Option,'\n');
    	if(Option=="SDW")
    	{
    		std::cout<<"Sorry,This option is not yet available."<<std::endl;
    	}
    	if(Option=="SDC")
    	{
    		std::string Dir=GetObject("DiskName");
    		if(Dir.length()!=3)
    		{
    			CheckLength(Dir);
    		}
    		File=Dir+"SecureDiskCleanse.txt";
    		bool FileExist=true;/*NEED TO WRITE THIS FUNCTION...*/
    		if(FileExist==true)
    		{
    			std::cout<<"Do you want to start where you left off?(Y or N):";
    			getline(std::cin,Restart,'\n');
    			if(Option=="Y"||"y")
    			{
    				RestartMainFunction(Dir,File);
    			}
    			else
    			{
    				NoRestartMainFunction(Dir,File);
    			}
    		}
            else
    		{
    			MainFunction(Dir,File);
    		}
    		Delete(File);
    		std::cout<<"Disk Cleansed."<<std::endl;
    		std::cin.get();
    	}
    	if(Option=="SFD")
    	{
    		int Loops,SizeOfFile,OrigSizeOfFile;
    		File=GetObject("File location");
    		OrigSizeOfFile=GetFileSize(File.c_str());
    		std::ofstream a_file(File.c_str(), std::ios::trunc);
    		Loops=GetFileSize(File.c_str());
    		while(Loops>0,Loops--)
    		{
    			a_file<<"1";
    		}
    		Delete(File);
    		std::cin.get();
    	}
    	else
    	{
    		std::cin.get();
    	}
    	return 0;
    }
    Also, if any of you have any suggestions to improve my code, I'm all ears.
    Last edited by bikr692002; 04-09-2006 at 08:07 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That looks good (although you don't have an implementation of Overwrite and your header needs include guards). Just make sure that source file is added to your project or makefile, and in your other source files (like main.cpp or something), just #include "..\Header\SecSuite.h" and use the functions. The IDE (or makefile) will compile main.cpp and that source file separately, and then link them together.

    Are you having a specific error? Perhaps multiple definition errors because the include guards are missing?

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Yeah, the linker is saying its all "undefined refrence to"PUTMAINFUNTIONHERE

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Wait... so how do I link all the files together? Sorry for my nievity its just I have everything in 3 files all together... the main,header,and funtions and the header is #include in the main and funtion files, but it doesnt compile (linker error)

    I swear someone posted between my two posts...


    Alright... I put them in a project and it worked...
    Last edited by bikr692002; 04-09-2006 at 08:26 PM.

  9. #9
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    i really wish i could help you there, but i do my development on linux so i'm not sure how to make it work in windows, specially if you are using some kind of IDE. You just must tell the linker to link the resulting objects file, which would be main.o and functions.o (the header is no longer used)

  10. #10
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    This is a try at explaining the use of header files for file compilation in a really simple and possibly not 100% accurate explanation of the compilation process:
    • The compiler is set to compile a given file
    • compiler parses the whole file and reaches a call to a function that is not defined in the file he is trying to compile, thus he can't check if arguments respect the function needs, this would mean a compiler error.
    • so you #include a header file with the prototype of the function you will be calling on this file (but is not defined on this file) so that the compiler can see if the call you make in this file respects the return type, and arguments that the actual function needs
    • later at linking stage all the compiled files (usually .o files) are linked together and the linker will replace all the calls for an actual address in memory (an offset from the place where the program is loaded). This is where it will actually try to get to the real compiled function code. if the function prototype in the header file is different from the actual function you won't get compilation errors but instead a linker error at this step.
    • If all went well you now have a new binary file or lib ready to use.
    Hope this can help you understand the use of header files a little better.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    What IDE (if any) are you using? For most people, if you add all the cpp files to the project, the IDE will compile them all and link them automatically when you build. As long as one and only one of them has a main function it should work.

    The undefined reference usually means that one of the files (probably the one with the function definitions)has not been added to the project or the makefile. How to fix it depends on how you are compiling.

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I had to make a project file and include all three files in it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 03:49 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21