Sponsered Answers

c++ call function from another source file?

Apr

9

2009

C++ is really confusing me. Im working on a project with a lot of files: source codes header files libraries. It is a jungle with tons of files classes and structures that must be defined somewhere. Im new to C

What type of comunication can exist between C files? Can you declare classes or objects in one file and use it in another one? Or is it best to use functions? And can you call a function in one file from another?

In: Computers & Internet Asked By: [ Grey Star Level]
Answer #1

include file path

Answers Answered By: freenaro vefff [ Grey Star Level]
Answer #2

You define your function prototypes and/or class definitions in the header files and then implement them in the source files. The compiler is run against each source file and the included headers for each source file are copied into the source file. Then the compiler creates object/compiled code for the source file and spits out an object file. If another source file also includes the header file with functions implemented in another object file the linker is provided with this information so that the external symbols can be resolved during linking. When you use an IDE such as Visual C this tedious stuff normally seen in a make file is all handled for you.

Basically just remember to put the definitions in the headers and implementation in source.

Example:

Stack.h
ifndefSTACK
defineSTACK

struct StackData

char Data
StackData Node

class Stack

private:
StackData mHead

public:
Stack
virtual Stack
void Pushchar Data
char Pop void
bool IsEmpty void

endif

Stack.cpp

include Stack.h

ifndef NULL
define NULL0
endif

Stack::Stack: mHeadNULL / Constructor /

// Destructor
Stack::Stack

whilemHead NULL

StackData OldNode mHead
mHead mHeadNode

delete OldNode

void Stack::Pushchar Data

StackData NewData new StackData

NewDataData Data
NewDataNode NULL

if mHead NULL

mHead NewData

else

NewDataNode mHead
mHead NewData

char Stack::Pop void

char Result 0

ifmHead NULL

Result mHeadData

StackData OldData mHead
mHead mHeadNode
delete OldData

return Result

bool Stack::IsEmpty void

return mHead NULL

main.cpp

include iostream
include string
include string.h
include Stack.h

using namespace std

void main void

string sInput
string sOutput
Stack oStack

cout Enter a string: endl
cin sInput

for sizet i 0 i sInput.length i
oStack.PushsInputi

whileoStack.IsEmpty
sOutput oStack.Pop

if sInput.comparesOutput 0
cout It is a palindrome. endl
else
cout It is NOT a palindrome. endl

Answers Answered By: quick lick [ Grey Star Level]
Answer #3

You can include filename.cpp to obtain a classes/functions but I consider it poor programming. Typically you want to include only the header and link additional source file.

Answers Answered By: ttreeee [ Grey Star Level]

Answer this Question

You must be Logged In to post an Answer.

Not a member yet? Sign Up Now »

Star Points Scale

Earn points for Asking and Answering Questions!

Grey Sta Levelr [1 - 25 Grey Star Level]
Green Star Level [26 - 50 Green Star Level]
Blue Star Level [51 - 500 Blue Star Level]
Orange Star Level [501 - 5000 Orange Star Level]
Red Star Level [5001 - 25000 Red Star Level]
Black Star Level [25001+ Black Star Level]

Powered by Yahoo! Answers