GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jan-2007, 09:34
Taichichuan Taichichuan is offline
New Member
 
Join Date: Jan 2007
Location: Swansea
Posts: 7
Taichichuan is on a distinguished road

help : error C2065: 'information' : undeclared identifier


This is going to be alot of code in this post lol. Right basically I have only now got used to header files in C, but im having problems with myLib.cpp. Here are the errors that are being displayed.

Code:
yLib.cpp error C2065: 'information' : undeclared identifier error C2065: 'customers' : undeclared identifier error C2062: type 'char' unexpected error C2143: syntax error : missing ';' before '{' error C2447: '{' : missing function header (old-style formal list?)

Now here is my header file callwed myLib.h

CPP / C++ / C Code:
#ifndef unique_symbol  //stop the header being complied multiple times
#define unique_symbol

// Library Structures

typedef struct
{
	char name[20];
    int age;
    char gender[7];
    float purchaseCost;
    float purchaseCount;
}information;

information customers[20];

void setEmployee(information *customers, char *name, int age);


#endif

here is my cpp file called myLib.cpp

CPP / C++ / C Code:
#include "myLib.h"
#include <stdio.h>
#include <string.h>
#include "stdafx.h"


//Bodies
//void printEmployee(information *customers)
//{
	//printf("NAME- %s\n",customers[i].name);
	//printf("AGE- %f\n",customers[i].age);
//}

void setEmployee(information *customers, char *name, int age)
{
	customers[i].name="Jamie";
}

and at last here is my main code

CPP / C++ / C Code:
#include <stdio.h>
#include "stdafx.h"
#include "myLib.h"



void write_struct (const information* customers, const int count);
void read_struct( const information* customers, const int count );

int main()
{
	int i=0;
	
   // int number;
	
		setEmployee(&customers[i],"Jamie",20);
		//printf("\nPlease enter the number of customers you would like to enter\n");
		//scanf("%d",&number);
		//write_struct (customers, number);
		//read_struct( customers, number );

   // return 0;
}

void read_struct( const information* customers, const int count )
{
    int i;
    
    for( i=0;i<count;i++ )
    {
        printf("\nCustomer Name\n");
        printf("%s \n", customers[i].name);
        printf("\nCustomer age\n");
        printf("%d \n", customers[i].age);
        printf("\nCustomer gender\n");
        printf("%s \n", customers[i].gender);

    }
}
void write_struct (const information* customers, const int count)
{
	int i;

	for(i=0;i<count;i++)
	{
		 printf("\nPlease enter a customer name\n");
        scanf("%s", customers[i].name);
        printf("\nPlease enter the customers age\n");
        scanf("%d", &customers[i].age);
        printf("\nPlease enter the customers gender (Male or Female)\n");
        scanf("%s", customers[i].gender);
	}
}

Now what i am trying to do is actually for the first time use a header file with cpp file to make the main as little as possible. So i can print the structure and create the structure with my own functions. but i keep getting the errors, Can someone have a quick skim through my code and try to target the problem areas. I am realtivly new to programming and have tried for hours to fix the errors with no luck. Thanks in advanced
  #2  
Old 23-Jan-2007, 11:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help : error C2065: 'information' : undeclared identifier


Quote:
Originally Posted by Taichichuan
CPP / C++ / C Code:
#include "myLib.h"
#include <stdio.h>
#include <string.h>
#include "stdafx.h"


I believe that the Microsoft compilers ignore everything before the #include "stdafx.h" statement.

Suggestion #1:
Try again with #include "stdafx.h" as the very first line in whatever files use it.

Suggestion #2:
Until you really get into situations where compile time is an issue, I suggest that you see if you can make projects that don't include stdafx.h. This requires a change from the default project settings.

The choice not to use precompiled headers is somewhere in your projects->properties menu, I think.

In Visual C++ 2005 express it can be reached as follows:

Right-click on the project name in the "Solution" panel, then from the pop-up context menu select
properties->Configuration Properties->C/C++->Precompiled Headers
The top item in the box that opens up is "Create/Use Precompiled Headers" You should be able to select "Not Using Precompiled Headers". (And, of course, delete the #include "stdafx.h" statements in your source file(s).)

It might be somewhat different for other versions of compiler.

I personally recommend the second one, since many people who follow these threads might like to compile your programs and they are not using a Microsoft compiler with stdafx.h. (That might help some beginners, and it also might help people who are trying to help you.)

Regards,

Dave
  #3  
Old 23-Jan-2007, 12:19
Taichichuan Taichichuan is offline
New Member
 
Join Date: Jan 2007
Location: Swansea
Posts: 7
Taichichuan is on a distinguished road

Re: help : error C2065: 'information' : undeclared identifier


Thanks a hell of a alot. Really helped me out. Once i cleared those errors i could guid my self from other errors Thanks alot. One more thing I only receive one error now during the linking process. And i think its the only thing thats stopping it from working.

Code:
orksheet4(Q2).obj : error LNK2005: "struct information * customers" (?customers@@3PAUinformation@@A) already defined in myLib.obj C:\Documents and Settings\Taichichuan.JAMIE\My Documents\Visual Studio 2005\Projects\Worksheet4(Q2)\Debug\Worksheet4(Q2).exe : fatal error LNK1169: one or more multiply defined symbols found

I dotn have a clue what i t means. Any advice would be great. Ill post my new code again just incase its something in there. (The main is very small now)

myLib.h
CPP / C++ / C Code:
#ifndef unique_symbol  //stop the header being complied multiple times
#define unique_symbol

// Library Structures

typedef struct
{
	char name[20];
    int age;
    char gender[7];
    float purchaseCost;
    float purchaseCount;
}information;

information customers[20];

void setinfo (const information* customers, const int count);
void readinfo (const information* customers, const int count);

#endif

myLib.cpp
CPP / C++ / C Code:
#include "stdafx.h"
#include "myLib.h"
#include <stdio.h>
#include <string.h>



//Bodies


void setinfo (const information* customers, const int count)
{
	int i;

	for(i=0;i<count;i++)
	{
		 printf("\nPlease enter a customer name\n");
        scanf("%s", customers[i].name);
        printf("\nPlease enter the customers age\n");
        scanf("%d", &customers[i].age);
        printf("\nPlease enter the customers gender (Male or Female)\n");
        scanf("%s", customers[i].gender);
	}
}

void readinfo (const information* customers, const int count)
{
	int i;

	for(i=0;i<count;i++)
	{
		 printf("\nPlease enter a customer name\n");
        scanf("%s", customers[i].name);
        printf("\nPlease enter the customers age\n");
        scanf("%d", &customers[i].age);
        printf("\nPlease enter the customers gender (Male or Female)\n");
        scanf("%s", customers[i].gender);
	}
}

main
CPP / C++ / C Code:
// Worksheet4(Q2).cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <stdio.h>
#include "myLib.h"

void main()
{
	int i=0;
	int number;

		printf("\nPlease enter the number of customers you would like to enter\n");
		scanf("%d",&number);
		setinfo(customers,number);
		
	  
}
  #4  
Old 23-Jan-2007, 13:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help : error C2065: 'information' : undeclared identifier


Quote:
Originally Posted by Taichichuan

Code:
orksheet4(Q2).obj : error LNK2005: "struct information * customers" (?customers@@3PAUinformation@@A) already defined in myLib.obj C:\Documents and Settings\Taichichuan.JAMIE\My Documents\Visual Studio 2005\Projects\Worksheet4(Q2)\Debug\Worksheet4(Q2).exe : fatal error LNK1169: one or more multiply defined symbols found

myLib.h
CPP / C++ / C Code:
information customers[20];

myLib.cpp
CPP / C++ / C Code:

#include "myLib.h"

main
CPP / C++ / C Code:
#include "myLib.h"

Each source file is compiled as a separate "compilation unit". As each file is compiled, an "object file" is created that holds information about the code and all of the variables that were defined. To create an executable file, the linker puts together everything from the object files.

So Here's the Poop:

1. myLib.cpp is compiled. One of the things that happens is that a variable named customers is created. (An array of information thingies.) Storage and linkage information for the array customers is put into myLib.obj, along with the executable code from myLib.cpp

2. The main (worksheet4.cpp) file is compiled and creates its own variable named customers, which is an array of information things. The variable linkage and code from worksheet4.cpp is stored in an object file, named worksheet4.obj

3. The linker tries to tie together the code and variables in the two object files and finds that the variable customers is defined more than once. Thus the error message.

The solution is to refrain from putting any actual storage allocation in a header file that will be used in more than one place. Note that your inclusion guard definition in the header file protects it from being included more than once in a given compilation unit, but can not prevent the kind of error that you have seen: including it in more than once in different source files is a no-no since it actually allocates storage for a variable.

(Even though both files are in the same project for your Microsoft Visual C++ installation, the individual files are compiled separately, as required by the standard C and C++ language definitions.)

One possible solution:

1. Change the header file so that the variable is declared extern.

myLib.h
CPP / C++ / C Code:
extern information customers[20];

2. Then in one (and only one) of the source files. (I vote for the one with main() in it) put the actual allocation statement

CPP / C++ / C Code:
#include "myLib.h"
information customers[20];



Now the variable is a global and can be easily used in any file that includes "myLib.h"

A better solution is to not make the variable a global. Declare it in main(), and define any functions that need to use it have a parameter list that allows the variable to be passed as an argument.

There are lots of reasons to avoid using global variables as a general programming practice. It is not actually "illegal" to have them, but is highly dis-recommended at this point in your development. I showed you how to use globals if you really need them, not because I think you should use them, but in hopes of helping you understand the overall compile/link process that takes place when you build a program.

In fact, you have already done most of what I just recommended. Your functions setinfo() and readinfo() have parameters named "customers" and code inside those functions will access the value of the "customers" parameter passed to them as an argument. (So they wouldn't even see the global "customer" variable anyhow.)

Bottom line:
1. Remove the "customers" declaration from myLib.h.

2. Put the "customers" inside the main() function. Just after the opening brace "{" of the main() function would be as good a place as any.

Regards,

Dave
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fraction program SpudNuts C++ Forum 6 25-Sep-2006 03:50
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 16:43
Computer privacy article crystalattice Open Discussion Forum 0 01-Oct-2004 14:26
Help! undeclared identifier error wfillis .NET Forum 8 25-Aug-2004 06:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.