Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function problem

Status
Not open for further replies.

GiSH

Technical User
Oct 31, 2001
7
JP
Ok I'm usisng borland c++ builder 5
And I've got a character string that is declared in my main program file (szStringBuff)
Which is read into a data array for use in the program using



My problem I have a separate .cpp file set up in which I want to use this data but I can’t seem to get any functions to work to carry it across.

Any ideas, basic tips of smacks around the head
 
well, there are a few options. you can write functions that receive char szStringBuff[] or char* szStringBuff.

Another option is to declare it globally in a .cpp file, make a header file with "extern char szStringBuff[#];" in it and everywhere you want to use it include the header file. This could be more dangerous if multi-threading is involoved.

Matt
 
I've been trying to get a function to work in my seperate file function declared and defined and the file i'm using (unit2.cpp) is included in the main file along with it's header.
compiles, but won't link giving "unresolved external (the function) refrenced from main.obj" as the reason for the linking error.

if i declare'd it as a golbal variable in my main header file then jus called it (remembering to include it) from my 'unit2' file will it make the program unstable?
 
I dont fully follow what you are saying... here is how it should look

/*unit2.h*/
#ifndef _UNIT2_H_
#define _UNIT2_H_

// declaration of funciton(s)
void example(char* str);

#endif

/*unit2.cpp*/
#include "unit2.h"

// definition of function(s)
void example(char* str)
{
cout<<&quot;Got the string &quot;<<str<<endl;
}


and then in the file with the main

#include &quot;unit2.h&quot;

void main()
{
char szStringBuff[32];
*szStringBuff = 0; // just something I do

strcpy(szStringBuff,&quot;This is a test&quot;);
// call funcitons in unit2
example(szStringBuff);

}

I hope this clarifies things a bit
 
This is kinda what I've been doin. I've tried your stuff too and (I'm using Borland c++ builder 5) it complies and links when everything is declared but the function's not used in the main file. as soon as that's done it comes up with a warning of &quot;Style of function declaration obsolete&quot;
and a c++ error saying &quot;{&quot; is expected when it blatantly isn't cos it's a function call not a declaration or definition.

argh
 
Ah panic over
managed to get it work after all
jus weird trying to code in BCB5, idon't really like some of the syntax but it's working now

cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top