Jump to content
  • Advertisement
Sign in to follow this  
twystidmynd

Undeclared Identifier Error

This topic is 4421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm familiar with the standard error, which usually means that I've got some variable I'm using that just hasn't been declared yet, but my problem is a little more complicated. I've got two files I'm dealing with. The first file is a binary file that just holds a long string of 0's and 1's that can be translated into a Matrix of values. The second file is a text file that tells me what data type those values are; they could be 8-bit, 16-bit, 32-bit, or 64-bit. I am then trying to read in 100 numbers from the first file, but I can only tell when to stop reading if I know the byte-size of the data in the file (like, if the data is stored as 64-bit numbers, then I've gotta read in 800 bytes of info, whereas I'd only have to read in 100 bytes if the values were 8-bit). To accomplish this task, I'm currently doing this (where dataTypeSize is already known to be the number of bytes used to define an individual value in the first file):
if(dataTypeSize == 1) //Because a char is 1-byte
    char * tempBands = new char[100];
else if(dataTypeSize == 2) //because a short is 2-bytes
    short * tempBands = new short[100];
else if(dataTypeSize == 4) //because an int is 4-bytes
    int * tempBands = new int[100];
else if(dataTypeSize == 8) //because a double is 8-bytes
    double * tempBands = new double[100];
else //Since the default size is 2-bytes
    short * tempBands = new short[100];
memset(tempBands, 0, 100*dataTypeSize);
The problem is that, at the memset() function, my compiler is yelling at me, saying that tempBands isn't declared (which makes sense, since there's no guarantee that those if statements will ever evaluate to true), but *I* know that there are no other options. dataTypeSize will ALWAYS be one of those 4 values, so tempBands will ALWAYS be declared. So, how do I convince my compiler that tempBands is definitely declared? Evidently my compiler doesn't understand my version of Trichotomy! (quadchotomy?)

Share this post


Link to post
Share on other sites
Advertisement
You're declaring variables in a different scope, and it's not in the scope of the "memset" call. The compiler won't let you do this since it won't know the definite type due to the run-time input.
As for the different memory types, you're going to have to interperet them as int's, short's, etc, when you read the data into a matrix. As for allocating memory:
char *tempBands=new char[100*dataTypeSize];

Share this post


Link to post
Share on other sites
Would there be a way to declare my own type of variable, like create a data type called MyData and declare it to be 4, 5, 6, 7, 8, 16, 64, 128 or however many bits I needed it to be, then say MyData * tempBands = new MyData[100]?

Share this post


Link to post
Share on other sites
Quote:
Original post by smitty1276
You could use a templated class that stores a type T and returns its size.


I'm curious to know what you mean by this. I did some looking around to figure out how Templated Classes work, and this is basically what I got (a simple example provided, for explanation):


template <class A_Type> class calc
{
public:
A_Type multiply(A_Type x, A_Type y);
A_Type add(A_Type x, A_Type y);
};
template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y)
{
return x+y;
}

int main(int argc, char * argv[])
{
int dataTypeSize = 4;
if(dataTypeSize == 4)
{
calc<int> abc;
int a =2;
int b =3;
int c = abc.add(a, b);
}
else if(dataTypeSize == 8)
{
calc<double> abc;
double a =2;
double b =3;
double c = abc.add(a, b);
}
/*cout<<a<<endl<<b<<endl<<c<<endl; HAS ERRORS*/
return 0;
}

This all works fine, compiles and all, but the problem is that I want to be able to access a, b, and c outside of those if-statements... and that's the problem I can't quite figure out how to solve =(

Share this post


Link to post
Share on other sites
That isn't possible because C++ is a statically typed language -- the type of a variable cannot be decided at run-time. Consider polymorphism, or a variant type such as boost::variant.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!