I want to declare my linked list as external in my header file..so that my head and current pointers are globals..i have done something like this but i dont know where the problem is..i want the values of head and current to be initialized and used by all the other fuctions without re-declaring them or re-initializing them...the code below compiles but current and head are not initialized..

#include<fstream>
using namespace std;
struct myStruct
{
	string name;

	double number;

	myStruct* link;
};

typedef myStruct* nodePtr;
extern nodePtr head;
extern nodePtr current;

void new_details(nodePtr& head, nodePtr& current);
void add_details(nodePtr& head ,nodePtr& current);

#endif

You simply need to initialize the pointers ...

nodePtr head = 0;
nodePtr current = 0;

The extern declarations of the pointers you have there, do not impact the actual variables' values in any way, in case you were expecting that to happen.

> the code below compiles but current and head are not initialized..
You can't initialize something that doesn't exist. When you say that a variable is extern, you're saying that it's defined and initialized elsewhere. If you don't define and initialize current or head elsewhere, you'll probably get a linker error saying that those two variables are unresolved externals.

On a side note, using globals for your linked list is a bad idea. What if you want two lists?

actually i wanted something like this

nodePtr head=NULL;
nodePtr current=head;

;
but with this i am getting linker errors

but with this i am getting linker errors

There are quite many linker errors, which errors are you receiving?


On a side note, using globals for your linked list is a bad idea. What if you want two lists?

well i want the current node to remain unchanged..i.e i want to keep track of the current value..all i can do is assgn current to head every time i want to use current....this brings general results of the whole list and not the specific node am working with...
N.B ->these are all in different functions which are modularized

There are quite many linker errors, which errors are you receiving?

1>main.obj : error LNK2005: "struct myStruct * head" (?head@@3PAUmyStruct@@A) already defined in display.obj
1>main.obj : error LNK2005: "struct myStruct * current" (?current@@3PAUmyStruct@@A) already defined in display.obj
1>new_details.obj : error LNK2005: "struct myStruct * head" (?head@@3PAUmyStruct@@A) already defined in display.obj
1>new_details.obj : error LNK2005: "struct myStruct * current" (?current@@3PAUmyStruct@@A) already defined in display.obj
1>records.obj : error LNK2005: "struct myStruct * head" (?head@@3PAUmyStruct@@A) already defined in display.obj
1>records.obj : error LNK2005: "struct myStruct * current" (?current@@3PAUmyStruct@@A) already defined in display.obj
1>.\Debug/main_prog.exe : fatal error LNK1169: one or more multiply defined symbols found

Move the two lines to a .cpp file, now they seem to be in a header file.

I.e. these two lines ...

nodePtr head=NULL;
nodePtr current=head;

ok...seems like it was a bad idea afterall...i seem to be getting an infinite loop or something..do you know anyway i can keep track of like the current record in one function and use it in another

> do you know anyway i can keep track of like the current record in one function and use it in another
Edward highly recommends encapsulating all of the data you need into a separate list object:

struct list {
  nodePtr head;
  nodePtr current;
};

Then you can pass around the list object and have access to all of the data you need without losing flexibility or making the code too complicated.

actually i ended up using something so basic...i assigned the current item in each function to one variable that will be passed in each function...alot more easier :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.