|
an alternative to such a behavior is to overload the operator () instead, but i conceed, you will have a fixed number of dimension...
i don't think overloading the operator [] is possible for such a thing, because look :
if you have myType t[10];
[] will work on a myType array (every thing fine so...)
but with myTpe t[10][20];
the second [] will work on a myType[10] array... now, everything goes hard because you would have to overload as many versions of the [] as you want for the first dimension !!!
imagine myType[10][20][30];
the 3rd [] works on a myType[10][20] array !!!!!!
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
toxcct wrote: i don't think overloading the operator [] is possible for such a thing,
Why? This will work for infinite dimensions.
class A
{
public:
A operator [](int index)
{
A a;
return a;
}
};
Of course, it'll be tough to code the body of the function, as you won't know which dimension you are in.
Oh, in myType[10][20], the second dimension operates on whatever was returned by the first [], so it's not really overloading for myType[10].
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
-- modified at 4:35 Saturday 5th November, 2005
|
|
|
|
|
For a 2-D situation the easiest thing to do is something like this:
<code>
Array of arrays of ints
typedef vector<vector<int> > CMultiIntArray;
void TraceDynamicArray(const unsigned int rows, const unsigned int columns)
{
CMultiIntArray aTest;
// Allocate number of rows
aTest.resize( rows );
// For each row
for (int row = 0; row < aTest.size(); row++)
{
// Allocate number of columns
aTest[row].resize( columns );
// For each column
for (int column = 0; column < aTest[row].size(); column++)
{
// Assign a value
aTest [row] [column] = 10 * row + column;
// Trace it
cout << aTest [row] [column] << "\t";
}
cout << "\n";
}
}
// Example usage: Standard C++ Library version
int main(int argc, char* argv[])
{
TraceDynamicArray(3,5);
return 0;
}
// For rows = 3, columns = 5, produces output...
// 0 1 2 3 4
// 10 11 12 13 14
// 20 21 22 23 24
</code>
Then you can do something similar for higher dimensions. But you have to define the dimensions upfront.
Kevin
|
|
|
|
|
I have a question. I wrote and array of 5 structs into a binary file. I need to be able to go to a specific position in a binary file and overwrite that struct with another. I have tried it but when i go to overwrite the struct the ones before it all the data is screwed up.Can anyone help me on how to overwrite a specific person in the file.
struct PERSON
{
int age;
int weight;
char name[25];
};
void write_names() //WRITES 5 PEOPLE TO THE FILE
{
ofstream out;
PERSON p[5];
int ct;
p[0].age = 1;
p[0].weight = 1;
strcpy(p[0].name, "Num_1");
p[1].age = 2;
p[1].weight = 2;
strcpy(p[1].name, "Num_2");
p[2].age = 3;
p[2].weight = 3;
strcpy(p[2].name, "Num_3");
p[3].age = 4;
p[3].weight = 4;
strcpy(p[3].name, "Num_4");
p[4].age = 5;
p[4].weight = 5;
strcpy(p[4].name, "Num_5");
out.open("file.dta", ios::binary);
for (ct=0; ct<5; ct++)
out.write((char*)& p[ct], sizeof(p[ct]));
out.close();
}
void show_names() //SHOW ALL THE PEOPLE IN THE FILE
{
ifstream f;
f.open("file.dta", ios::binary);
PERSON p[10];
int ct;
int num = 0;
while(!f.eof())
{
f.read((char*)& p[num], sizeof(p[num]));
num++;
}
num--;
for(ct=0; ct
|
|
|
|
|
you need to use ios::binary|ios::_Nocreate as the open mode when you do the open to replace the second person
|
|
|
|
|
Basically I have two programs... the first program as you to input a total number of variables you want... max of 8.. then it ask you for its value... after it does all that it ask you if you want to add subtract mult or divide... then once all that is done it out puts to a file of your choice(what you name it)
LOOKS LIKE
a 1
b 2
c 3
d 4
e 5
f 6
g 8
h 10
add
a b
sub
d e
mul
e a
div
c d
and so on.... my problem is in my second program im trying to read the values in the being able to read in the command add sub div or mul... i attempt while(!fin.eof()) and read in the opperation add,sub and so on then the 2 variables like a b c d e f and so on... then run a bunch of if statesments to test if they match and once it does it outputs the answer.. the i want it to go to the next line...and read in next opperation and 2 variables and compute but it usually fails...
heres sample code...
#include <iostream>
#include <fstream>
#include <iomanip>
//dynamic memory
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
char filename[20];
int pause,x,y,z;
int int1,int2,int3,int4,int5,int6,int7,int8;
char char1,char2,char3,char4,char5,char6,char7,char8;
string opp1[x];
char aa[x];
char bb[x];
x=0;
cout <<"Please enter the filename that you created to store"<<endl;
cout="" <<"information="" in="" the="" first="" program.="" ex:="" c:\data.txt"<<endl;
="" cin="">>filename;
fin.open(filename, ios::in);
cout<<"This is my awesome program:"<<endl;
if="" person="" doesnt="" enter="" all="" 8="" variables="" then="" they="" fill="" with="" zeros
="" int1="0;
" int2="0;
" int3="0;
" int4="0;
" int5="0;
" int6="0;
" int7="0;
" int8="0;
" char1="0;
" char2="0;
" char3="0;
" char4="0;
" char5="0;
" char6="0;
" char7="0;
" char8="0;
" fin="">> char1 >>int1;
fin >> char2 >>int2;
fin >> char3 >>int3;
fin >> char4 >>int4;
fin >> char5 >>int5;
fin >> char6 >>int6;
fin >> char7 >>int7;
fin >> char8 >>int8;
cout <<char1 <<"=" <<int1 <<endl;
cout <<char2 <<" =="" "="" <<int2="" <<endl;
="" cout="" <<char3="" <<int4="" <<char5="" <<int6="" <<char7="" <<int8=""
="" while(!="" fin.eof())
="" {
="" fin="">> opp1[x];
//cout <<opp1[x] <<endl;
="" fin="">>aa[x];
//cout <<aa[x]<<" ";
="" fin="">>bb[x];
//cout <
|
|
|
|
|
Hello,
i a little newbie to C++. So sorry for some stupid questions.
The following scenario:
We have a java application running on 200 pcs in a network enviroment. If a user starts the app the complete jre has to be loaded from a mounted network volume. This took about 20 to 40 sec. We cannot change this...
So currently i'm trying to write a splash screen which is showing the user that something is going on.
The splash screen is implemented. Now my question. Is there an easy way to lauch a jar as a process from c++ in that way that the splash screen waits until the java app is started?
Are ther tutorials in the web available?
Thanks for any help.
Regards,
Carsten
University of Konstanz
|
|
|
|
|
if you have a console command that can be launching your java app, you could so easily call
ShellExecute(NULL, "open", "myCommandLine here", NULL, NULL, SW_SHOWMINIMIZED);
or
system("myCommandLine here");
TOXCCT >>> GEII power [toxcct][VisualCalc]
-- modified at 5:46 Wednesday 2nd November, 2005
|
|
|
|
|
Hi,
to launch my java app this works... but how do i wait then with my c++ app until the app/process is really completed?
Thanks for any help.
Regards
Carsten
|
|
|
|
|
i'm not sure, but i think of deporting this code into a worker thread... see if you don't find some infos on the web on that subject.
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
|
Hi all,
I have a very important problem. that How to judge a string encoding is euc, sjis, ascii or others?
I must alway kown a string encoder, and I must convert it to other encoder.
so first, I must know the encoder of a string.
thank you very much.
thanks a lot.
---------------------------
Vc++.net Managed
|
|
|
|
|
a function that judge a string is ASCII or EUC, SJIS, JIS was welcome.
thanks.
----Junyin.Wu
|
|
|
|
|
there is no way to know such an information programatically... sorry
there is only one thing you can know : if the length of the string have an odd number of bytes, it cannot be UNICODE
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
try IMultiLanguage2::DetectCodepageInIStream
http://blog.joycode.com/jiangsheng
http://blog.csdn.net/jiangsheng
http://bloglines.com/public/jiangsheng
Command what is yours
Conquer what is not
---Kane
|
|
|
|
|
thank you very much,
and we are trying.....
|
|
|
|
|
Of course it can be Unicode with an odd number of bytes. In Unicode text encoded with UTF-8, the characters are composed of byte sequences of sizes between 1 and 4 bytes, depending on the character. Perhaps you meant UTF-16 or UTF-32?
--
The Blog: Bits and Pieces
-- modified at 1:40 Tuesday 1st November, 2005
|
|
|
|
|
|
Hi,
Being relatively new to socket programming, I need some assistance in sending and receiving data over a TCP connected socket. I'm familiar with using send and recv commands to send and recv buffered character data, but what about if I want to send and recv data formatted within a structure? For example,
<code>
main ()
{
struct {
int data1;
double data2;
bool indicator;
}msg;
// initialize data
msg.data1 = 0;
msg.data2 = 5.8;
msg.inidicator = false;
// send data
.....
// receive data
.....
}
</code>
Are the send and recv commands the right ones to use for this type of information to send and receive data over a TCP connected socket or are there other commands that are better suited for this. When I use the send(socketfd, (char *)&msg, 0), it "appears" to work, but when receiving the data using recv(socketfd, (char *)&msg, 0), it returns with a -1.
Thanks in advance for any help.
-Martin
NB: The commands must work on both the Windows and UNIX side, so Windows specific commands wouldn't work.
|
|
|
|
|
you are sending 0 bytes. you need to give it a size that your are sending. You also need to give a size when you recv.
It would work best if you just used sizeof(msg)
|
|
|
|
|
just check out the following code:
<br />
main()<br />
{<br />
int a=32;<br />
int b=0x20;<br />
printf("%d %d",a,b);<br />
getch();<br />
}<br />
the o/p of this code as expected would be -->32 32 now my question is that if i want the o/p to be like this-->32 0x20 what should i do.i am open to suggestions in C++ also.
plz ans asap.
thank you.
bye
That is a good way of doing difficult thing easily sir but i dont want to use specifier. i want to use the method of sepration which is used by compiler itself. try to differentiate between the two using functions atoi()
and itoa().
however thank you for your time sir and plz pardon me for my english
amsbali@yahoo.co.in
-- modified at 7:49 Sunday 30th October, 2005
|
|
|
|
|
just change your format string :
printf("%d %#X", a, b);
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
that is a good way of doing difficult thing easily sir but i dont want to use specifier. i want to use the method of sepration which is used by compiler itself. try to differentiate between the two using functions atoi()
and itoa().
however thank you for your time sir and plz pardon me for my english
|
|
|
|
|
two things :
1) you never said you didn't wan't the way i gave you
2) you say you solved it, but you don't give any feedback here on how you did it...
i'm curious to know how, because i don't really see how this is possible (or i did not understood the question).
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
What seems to be the problem here is that you don't realize what compile-time and run-time means. What you are trying to accomplish is as far as I know impossible. Once the compiler parses the line int b=0x20; it automatically defines a symbol in the data stack and initializes that space with the value of 0x20, which in base 10 is 32. The compiler however does not store any base information along with the value. Hence, the problem you are posing is unrealistig to say the least. Maybe I don't understand your question, but it really seems to me that you aren't really asking the right question.
Note that most answers are found by simply asking the right question!
I'm waiting for more information on the subject!
(also, as a note -- I think this is a post that doesn't necessarily belong in the C++/CLI area)
|
|
|
|