|
In a header file of one of my classes I have a private two dim array,
<br />
double V[][6] = {<br />
{-.01,0,0, -.2,-.2,1},
{.1,0,0, .2,-.2,1},<br />
{0,.11,0, -.2,.2,1},<br />
{.08,.09,0, .2,.2,1}<br />
};<br />
but this gives me a compiler error. It says "syntax error : '{'" and
"unexpected token(s) preceding '{'; skipping apparent function body".
Does anyone know why? All I want to do is declare and initialize it.
Also, lets say I have a 3-dim array, (which I cant initialize either until I fix my first problem)
<br />
double polygon[][][] = {<br />
{ V[0], V[1], V[2] },<br />
{ V[1], V[2], V[3] }<br />
};<br />
and I need a for loop to go thru polygon. I want to do something like:
<br />
for (int n = 0 ; n < polygon.length ; n++)<br />
for (int i = 0 ; i < polygon[n].length ; i++) {<br />
for (int k = 0 ; k < 3 ; k++)<br />
normal[k] = polygon[n][i][k+3];<br />
What do I replace the bold & underlined with? Would it be sizeof(polygon)/sizeof(polygon[0]) for the outer loop and sizeof(polygon[0])/sizeof(polygon[0][0]) for the inner?
This stuff confuses me.
Thanx for the help,
-Flack
|
|
|
|
|
I got no errors with your two-dimensional array.
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Here is the entire header file:
<br />
<br />
#include <windows.h><br />
#include <math.h><br />
#include "Pix.h"<br />
<br />
class ZBuffer1 {<br />
<br />
private:<br />
double V[][6] = {<br />
{-.01,0,0, -.2,-.2,1},
{.1,0,0, .2,-.2,1},<br />
{0,.11,0, -.2,.2,1},<br />
{.08,.09,0, .2,.2,1}<br />
};<br />
<br />
double polygon[3][3][3];<br />
<br />
double L[1][3];<br />
<br />
double rgb[3];<br />
double p[30][7];
<br />
double FAR_Z;
double * zbuffer;<br />
double normal[3];<br />
<br />
int W, H;<br />
<br />
void init();<br />
<br />
void clearZbuffer();<br />
<br />
void projectVertex(double pt[], double p[]);<br />
<br />
void doShading(double pt[], double rgb[]);<br />
<br />
void displayPolygon(double polygon[][3]);<br />
<br />
<br />
void normalize(double v[]);<br />
<br />
double lerp(double t, double a, double b) {return a + t * (b - a);}<br />
<br />
int f2i(double t) {return max(0, min(255, (int)(255 * t)));}<br />
<br />
public:<br />
Pix pix;<br />
<br />
ZBuffer1(int, int);<br />
void setPix();<br />
};<br />
I try to compile and still get an error on the underlined line:
zbuffer1.h(14) : error C2059: syntax error : '{'
zbuffer1.h(14) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
I dont know what Im doing wrong.
|
|
|
|
|
Flack wrote:
I dont know what Im doing wrong.
You are trying to initialize that array within the class itself. Try this:
class ZBuffer1
{
private:
double V[][6];
public:
ZBuffer1();
};
ZBuffer1::ZBuffer1()
{
V[0][0] = -0.01;
V[0][1] = 0.0;
V[0][2] = 0.0;
V[0][3] = -0.2;
V[0][4] = -0.2;
V[0][5] = 1.0;
V[1][0] = 0.1;
V[1][1] = 0.0;
V[1][2] = 0.0;
V[1][3] = 0.2;
V[1][4] = -0.2;
V[1][5] = 1.0;
V[2][0] = 0.0;
V[2][1] = 0.11;
V[2][2] = 0.0;
V[2][3] = -0.2;
V[2][4] = 0.2;
V[2][5] = 1.0;
V[3][0] = 0.08;
V[3][1] = 0.09;
V[3][2] = 0.0;
V[3][3] = 0.2;
V[3][4] = 0.2;
V[3][5] = 1.0;
}
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
I think you have to first accept that this is not very easy to do in C/C++. A 2D array is an array of pointer to more arrays. In a declaration you can only have one unknown dimension []. So your second example will not compile. The first one might be an error related to
The rest must be specified. V is a pointer to a pointer, V[n] is a pointer and v[n][m] is a value. The [m] operator is saying advance i + k*m spaces to obtain the location.
You need to either dynamically allocate this with multiple news or allocs, or use constants to create the space in memory in the first place, so it is far easier just to store the dimensions in an integer and use this in your loops.
J.
----------------------------
|
|
|
|
|
JWood wrote:
You need to either dynamically allocate this with multiple news or allocs, or use constants to create the space in memory in the first place, so it is far easier just to store the dimensions in an integer and use this in your loops.
Or use C++.. vectors
John
|
|
|
|
|
Hey.
I was wondering if there was any way to get more than one line of text to appear in a static text control? I tried just putting a \n where I want the new line to start, but then any text after that is invisible.
So, I guess what I'm asking, is how do I turn:
43 text
into:
43
text
without using 2 separate static text boxes? (is this possible?)
|
|
|
|
|
|
*ahem*
thanks. stupid mistake. it was only large enough for one line, not both. haha. sorry!!!
|
|
|
|
|
Assuming a declaration of a character buffer:
char MyBuffer[10];
Will the following two statements produce the same results?
MyBuffer[5] = "x";
char* pMyBuffer = &MyBuffer;
pMyBuffer = pMyBuffer + 5;
*pMyBuffer = "x";
Thanks for your help,
|
|
|
|
|
I think you meant 'x' and not "x" . Also, you're assuming the size of a character is 1 byte (which is true for non Unicode/MBCS systems). Finally, I would initialize pMyBufferwrite like this (just to be explicit):
char* pMyBuffer = & (MyBuffer[0]);
...
Apart from this, yes.
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Ravi Bhavnani wrote:
Also, you're assuming the size of a character is 1 byte (which is true for non Unicode/MBCS systems).
AFAIK a char in C/C++ is always one byte in size. And his example uses char s not TCHAR s.
Regards,
Alvaro
Hey! It compiles! Ship it.
|
|
|
|
|
Alvaro Mendez wrote:
AFAIK a char in C/C++ is always one byte in size.
Good point!
I've been spending (way) too much time moving between CString s, char* and XMLCh in my (Unicode) app. My mind is in a fog (i.e. more than usual).
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Ravi Bhavnani wrote:
I've been spending (way) too much time moving between CStrings, char* and XMLCh in my (Unicode) app.
Perfectly understandable. I don't remember the last time I used char in an MFC app. It's practically extinct.
Regards,
Alvaro
Hey! It compiles! Ship it.
|
|
|
|
|
Not the way you have them. Here's how:
char MyBuffer[10];
// 1
MyBuffer[5] = 'x';
// 2
char* pMyBuffer = MyBuffer;
pMyBuffer = pMyBuffer + 5;
*pMyBuffer = 'x';
Regards,
Alvaro
Hey! It compiles! Ship it.
|
|
|
|
|
Thank you! You guys are a great help.
|
|
|
|
|
How can I use the Setup API functions with a INF_STYLE_OLDNT inf file to cause the PnP Manager to kick in? If I need to use a INF_STYLE_WIN4 inf file, what are the key Setup API functions?
|
|
|
|
|
I have a static filter using something like this:
TCHAR* tcFilter = _T("Word Document\0*.DOC\0Excel Document\0*.XLS\0");
What I really need is a dynamic filter where I can remove or add file types to it.
TCHAR* tcFilter = NULL;
if (IsWordInstalled())
etc.
Any suggestions on how I can do this? That '\0' delimiter really throws me for a loop.
"It is better to remain silent and be thought a fool than to open one's mouth and remove all doubt." - Abraham Lincoln
|
|
|
|
|
I think you'll need to use std::string to handle the embedded '\0' characters.
Otherwise, you'll have to get tricky with the memcpy() function.
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Correct.
One solution is memcpy().
Kuphryn
|
|
|
|
|
This works for me:
CString strFilter;
strFilter += ...
strFilter += (TCHAR)'\0';
dlgFile.m_ofn.lpstrFilter = strFilter;
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Would that handle multiple filters separated by '\0'?
"It is better to remain silent and be thought a fool than to open one's mouth and remove all doubt." - Abraham Lincoln
|
|
|
|
|
Jason Henderson wrote:
Would that handle multiple filters separated by '\0'?
Yes. You only need the \0\0 at the end from memory. Anyway this is how I do it in ED (see sig) to dynamically build the file types list.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
How about a little function that you can call for that:
int AddToFilter(TCHAR* szFilter, int nSize, LPCTSTR szDescription, LPCTSTR szExtension)
{
ASSERT(szFilter);
ASSERT(szDescription);
ASSERT(szExtension);
_tcscpy(&szFilter[nSize], szDescription);
int nLenDesc = _tcslen(szDescription);
_tcscpy(&szFilter[nSize + nLenDesc + sizeof(TCHAR)], szExtension);
int nLenExt = _tcslen(szExtension);
return nSize + nLenDesc + nLenExt + (sizeof(TCHAR) * 2);
}
Then you can use it like this:
TCHAR szFilter[300] = _T("");
int nSize = 0;
if (IsWordInstalled())
nSize = AddToFilter(szFilter, nSize, _T("Word"), _T("*.doc"));
if (IsExcelInstalled())
nSize = AddToFilter(szFilter, nSize, _T("Excel"), _T("*.xls"));
AddToFilter(szFilter, nSize, _T("All Files"), _T("*.*"));
I haven't tested (or even compiled) this, but hopefully it's close enough.
Regards,
Alvaro
Hey! It compiles! Ship it.
|
|
|
|
|
Whoa, thanks. I didn't expect you to write the code for me, but I'll give it a shot.
"It is better to remain silent and be thought a fool than to open one's mouth and remove all doubt." - Abraham Lincoln
|
|
|
|
|