starnge syntax errors in MS visual C++ 2003

Started by
1 comment, last by GameDev.net 17 years, 10 months ago
I am writing a library in C (not c++) that makes it easier to load targas and render them with opengl. I am checking the syntax by linking the library against a program that does nothing but return 0 and seeing if it compiles, but im getting 17 syntax errors that don't make any sense to me. here's the source code for the library, its header, and the main function: Targa.h
#pragma once


#include <tchar.h>
#include <time.h>
#include <wtypes.h>
#include <wingdi.h>
#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
//#include "Lobster.h"

#define ERROR_LIMIT 64
#define TRUE 1
#define FALSE 0 

typedef struct TARGA
{
	unsigned int xRes, YRes;
	char Error[ERROR_LIMIT];
	char* Data; //TODO: replace with a union for TGAs with non-chars (only if this turns out to be necessary)
	char  error[ERROR_LIMIT];
	short xOrigin;
	short yOrigin;
	short width;
	short height;
	short bpp;
	char  ImgOrigin;
	char  AlphaBits;
	char  format;
};

//Accepts a TARGA struct, and loads the data at path into it
int TgaInit(const char *path, TARGA *tga);

//Draws an array of Targas
int TgaDraw(TARGA *tga, int *xCoord, int *yCoord, /*unsigned*/ int number);

Targa.c:
#include "targa.h"





/**********************************
*
* TgaInit
* Loads a targa from path into tga
*
**********************************/
int TgaInit(const char *path, TARGA *tga)
{
	//Various stuff useful for loading a targa and not much else
	short      CM_Start;
	short      CM_Length;
	char       CM_EntSize;
	char       idLength;
	char       ImgDescriptor;
	char       CM_Type;
	char       ImgType;
	FILE       FilePointer;
	char       *CM_Data;


	FilePointer = fopen(*path, "r");

	fread(&idLength, sizeof(idLength), 1, &FilePointer);	
	fread(&CM_Type, sizeof(CM_Type), 1, &FilePointer);
	fread(&ImgType, sizeof(ImgType), 1, &FilePointer);

	if (ImgType < 1 || ImgType > 11)
	{
		strcpy(*tga.error, "Image is not a legal Targa");
		return FALSE;
	}

	fread(&CM_Start, sizeof(CM_Start), 1, &FilePointer);
	fread(&CM_Length, sizeof(CM_Length), 1, &FilePointer);
	fread(&CM_EntSize, sizeof(CM_EntSize), 1, &FilePointer);
	fread(&*tga.xOrigin, sizeof(*tga.xOrigin), 1, &FilePointer);
	fread(&*tga.yOrigin, sizeof(*tga.yOrigin), 1, &FilePointer);
	fread(&*tga.width, sizeof(*tga.width), 1, &FilePointer);
	fread(&*tga.height, sizeof(*tga.height), 1, &FilePointer);
	fread(&*tga.bpp, sizeof(*tga.bpp), 1, &FilePointer);
	fread(&ImgDescriptor, sizeof(ImgDescriptor), 1, &FilePointer);

	*tga.AlphaBits = ImgDescriptor & 0x0D;
	*tga.ImgOrigin = ImgDescriptor & 0x30;

	if (*tga.AlphaBits == 0)
		*tga.format = GL_RGB;
	else
		*tga.format = GL_RGBA;
	fseek(&FilePointer, idLength, SEEK_CUR);

	&tga.Data = malloc(*tga.width * *tga.Height * sizeof(*tga.Data));
	/*char*/ *CM_Data = malloc(CM_Length * CM_EntSize);//TODO: Add in an if statement to check for a CM before doing this
	//TODO: Work on adding support for more than just 8 bits per channel

	fread(&CM_Data, sizeof(CM_Data), 1, &FilePointer);
	fread(&Data, sizeof(*Data), 1, &FilePointer);
	if (CM_Type != 0)
	{
		//cm_to_true(*CM_Data);
		//TODO: Add color map support
		strcpy(*Error, "Color maps not supported");
		return FALSE;
	}

	return TRUE;
}



/****************************************
*
*TgaDraw
*Accepts an array of targas and draws them
*
******************************************/
int TgaDraw(TARGA *tga, int *xCoord, int *yCoord, unsigned number)
{
	unsigned int i;

	for (i = 0; i <= number; i++;)
	{
		glRasterPos2i(xCoord, yCoord);
		glDrawPixels(tga.width, tga.height, tga.format, GL_UNSIGNED_BYTE, tga.Data);
	}
	return TRUE;
}


and the main function:
[source lang=C]// lobster.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"

int _tmain(void)
{
	return 0;
}

here are my errors:
Quote: ------ Rebuild All started: Project: lobster, Configuration: Debug Win32 ------ Deleting intermediate files and output files for project 'lobster', configuration 'Debug|Win32'. Compiling... targa.c c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.h(35) : error C2143: syntax error : missing ')' before '*' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.h(35) : error C2081: 'TARGA' : name in formal parameter list illegal c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.h(35) : error C2143: syntax error : missing '{' before '*' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.h(35) : error C2059: syntax error : ')' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.h(38) : error C2143: syntax error : missing ')' before '*' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.h(38) : error C2143: syntax error : missing '{' before '*' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.h(38) : error C2059: syntax error : 'type' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.h(38) : error C2059: syntax error : ')' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(13) : error C2143: syntax error : missing ')' before '*' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(13) : error C2081: 'TARGA' : name in formal parameter list illegal c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(13) : error C2143: syntax error : missing '{' before '*' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(13) : error C2059: syntax error : ')' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(14) : error C2054: expected '(' to follow 'tga' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(83) : error C2143: syntax error : missing ')' before '*' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(83) : error C2143: syntax error : missing '{' before '*' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(83) : error C2059: syntax error : 'type' c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\targa.c(83) : error C2059: syntax error : ')' lobster.cpp Generating Code... Build log was saved at "file://c:\Documents and Settings\Owner\Desktop\C and C++\lobster (attempt 2)\lobster\Debug\BuildLog.htm" lobster - 17 error(s), 0 warning(s) ---------------------- Done ---------------------- Rebuild All: 0 succeeded, 1 failed, 0 skipped
i don't really see what im doing wrong, as some of these syntax errors say that i shouldn't put a ) in my function declarations. can anyone please help me?
Advertisement
I presume you understand the purpose of the syntax and just forgot the second TARGA:
    typedef struct TARGA    {        ...    } TARGA; 
FYI, you can also do it like this:
    struct TARGA { ... };    typedef struct TARGA TARGA; 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
I see quite a lot of problems with your code... Keep in mind that I haven't coded in C in some years:

- AFAIK there is no Visual Studio that features a C99 compiler, therefore // comments can't be in C code, use /* */ comments;

- your TARGA typedef isn't doing what you expect;

- the format member of the TARGA struct can't hold values like GL_RGB (they will overflow): you want GLint, IIRC;

- the signatures of TgaDraw() in the header and the implementation files don't match (one takes an unsigned, another a int);

- in TgaInit(), FilePointer should be a FILE*, but it's a FILE;

- in TgaInit(), you dereference "path" where you shouldn't;

- in TgaInit(), since FilePointer must be a FILE*, you don't want to take its address anymore in fread();

- in TgaInit(), you often derefence members of the tga pointer, apparently thinking that you are dereferencing the pointer itself. Instead of doing, say, *tga.error, use tga->error;

- in TgaInit(), you try to reference the Width member of the TARGA struct (uppercase 'W') but it has a width member (lowercase 'w');

- you dereference CM_Data to store the pointer returned by malloc, which is wrong;

- you attempt to fread into a variable called "Data", which doesn't exist. On the same line, you try to take that variable's size. I don't know if that's supposed to be the TARGA structure's Data member, or the variable CM_Data;

- in TgaInit(), you attempt to strcpy into a variable called "Error", which you dereference. I see no such variable declared anywhere. I assume it's the TARGA structure's Error, in which case you'll want to use tga->Error as above;

- TgaInit() sometimes returns TRUE or FALSE, but I don't see them typedef'ed anywhere. I could be wrong (probably am), but I don't think that any standard C header defines these, so you should probably check for them with ifndef and define them if they aren't defined;

- TgaDraw(), you have a ';' after "i++" in the for-loop.


That's all I could find from a purely syntaxic point of vue. I'm not saying the resulting code is correct, but with all the above fixes and by removing all the Windows-specific headers, I could build an object file.

The code I ended up with follows. Keep in mind that it's been modified to build. That is, I had to comment at least one line of your code (the one that references the inexistant "Data" variable), I removed some headers because I don't use Windows (I should have commented them, sorry) and added an include (SDL_opengl.h). It definitely doesn't mean that it behaves correctly.

If I may, I'd like to suggest that you pick a naming convention and stick to it. Also please avoid strcpy and family if at all possible (prefer the strncpy, or use the compiler-specific ones if portability isn't an issue). Finally, I'm afraid to say that you'll probably have to review pointers, as you seem a little confused by them. Manipulating pointers without understanding them is going to lead to terrible headaches at the very best. This is friendly advice and in no way meant to put you down.

/* targa.h */#ifndef IF260C3E0C600EA28A36E4E2FA546302B_627186979177454405#define IF260C3E0C600EA28A36E4E2FA546302B_627186979177454405#include <time.h>#include <stdio.h>#include <stdlib.h>#include "SDL.h"#include "SDL_opengl.h"/*#include "Lobster.h"*/#define ERROR_LIMIT 64#define TRUE 1#define FALSE 0 typedef struct {  unsigned int xRes, YRes;  char Error[ERROR_LIMIT];  char* Data;  char  error[ERROR_LIMIT];  short xOrigin;  short yOrigin;  short width;  short height;  short bpp;  char  ImgOrigin;  char  AlphaBits;  GLint  format;} TARGA;/*Accepts a TARGA struct, and loads the data at path into it*/int TgaInit(const char *path, TARGA *tga);/*Draws an array of Targas*/int TgaDraw(TARGA *tga, int *xCoord, int *yCoord, /*unsigned*/ int number);#endif /* IF260C3E0C600EA28A36E4E2FA546302B_627186979177454405 */


/* targa.cpp */#include "targa.h"/********************************** * * TgaInit * Loads a targa from path into tga * **********************************/int TgaInit(const char *path, TARGA *tga) {  /*Various stuff useful for loading a targa and not much else*/  short      CM_Start;  short      CM_Length;  char       CM_EntSize;  char       idLength;  char       ImgDescriptor;  char       CM_Type;  char       ImgType;  FILE       *FilePointer;  char       *CM_Data;  FilePointer = fopen(path, "r");  fread(&idLength, sizeof(idLength), 1, FilePointer);	  fread(&CM_Type, sizeof(CM_Type), 1, FilePointer);  fread(&ImgType, sizeof(ImgType), 1, FilePointer);  if (ImgType < 1 || ImgType > 11) {	strcpy(tga->error, "Image is not a legal Targa");	return FALSE;  }  fread(&CM_Start, sizeof(CM_Start), 1, FilePointer);  fread(&CM_Length, sizeof(CM_Length), 1, FilePointer);  fread(&CM_EntSize, sizeof(CM_EntSize), 1, FilePointer);  fread(&tga->xOrigin, sizeof(tga->xOrigin), 1, FilePointer);  fread(&tga->yOrigin, sizeof(tga->yOrigin), 1, FilePointer);  fread(&tga->width, sizeof(tga->width), 1, FilePointer);  fread(&tga->height, sizeof(tga->height), 1, FilePointer);  fread(&tga->bpp, sizeof(tga->bpp), 1, FilePointer);  fread(&ImgDescriptor, sizeof(ImgDescriptor), 1, FilePointer);  tga->AlphaBits = ImgDescriptor & 0x0D;  tga->ImgOrigin = ImgDescriptor & 0x30;  if (tga->AlphaBits == 0)	tga->format = GL_RGB;  else	tga->format = GL_RGBA;  fseek(FilePointer, idLength, SEEK_CUR);  tga->Data = malloc(tga->width * tga->height * sizeof(tga->Data));  /*TODO: Add in an if statement to check for a CM before doing this*/  /*char*/ CM_Data = malloc(CM_Length * CM_EntSize);  /*TODO: Work on adding support for more than just 8 bits per channel*/  fread(&CM_Data, sizeof(CM_Data), 1, FilePointer);  /*fread(&Data, sizeof(*Data), 1, FilePointer);*/  if (CM_Type != 0) {	/*cm_to_true(*CM_Data);*/	/*TODO: Add color map support*/	strcpy(tga->Error, "Color maps not supported");	return FALSE;  }  return TRUE;}/**************************************** * *TgaDraw *Accepts an array of targas and draws them * ******************************************/int TgaDraw(TARGA *tga, int *xCoord, int *yCoord, int number) {  unsigned int i;  for (i = 0; i <= number; i++) {	glRasterPos2i(xCoord, yCoord);	glDrawPixels(tga.width, tga.height, tga.format,				 GL_UNSIGNED_BYTE, tga.Data);  }  return TRUE;}



Hope this helps.

This topic is closed to new replies.

Advertisement