object array

hi everyone,
i have a problem with declaring array of objects `cause this isnt working...

1
2
3
4
SpellStats spell[10]={SpellStats(true, 5,  25, 8),
		      SpellStats(true, 50, 70, 7),
		      SpellStats(); SpellStats(); SpellStats(); SpellStats(); SpellStats(); SpellStats(); SpellStats(); SpellStats();			
                      };


This is class SpellStats

1
2
3
4
5
6
7
8
9
10
11
class SpellStats{
	private:
		int encost, cooldown, time;
		bool trained;
	public:
		SpellStats(){ trained=false; }
		SpellStats(bool NT, int EN, int CD, int NTime){
			trained=NT; encost=EN; cooldown=CD; time= NTime;
		}
		...
};


its causing this errors:
1st line: syntax error: '{' and
unexpected token(s) preceding '{'; skipping apparent function body

can I ask what have I wrong in this??
Last edited on
 
SpellStats(); SpellStats(); SpellStats(); SpellStats(); SpellStats(); SpellStats(); SpellStats(); SpellStats();


Above are part of an array definition so shouldn't we use a comma to separate them ?
yea, my mistake, but still causing the same errors :|
1
2
3
4
SpellStats spell[10]={SpellStats(true, 5,  25, 8),
                      SpellStats(true, 50, 70, 7),
                      SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats()
                      };


The last comma is not needed.
yes I know I made a mistake there but now I fixed it and its still causing errors...

1
2
3
4
5
SpellStats spell[10]={ 
                     SpellStats(true, 5,  25, 8),
		     SpellStats(true, 50, 70, 7),
		     SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats()
		     }


and still causing that errors
You can't initialize an array of objects like that. The default constructor is automatically called, and there is no way to call a specific one unless you, say, make an array of pointers that you dynamically allocate.
 
and still causing that errors 


Compilation or run-time errors ?
hmm...
so idk why it was writted on some tutorials it can be initialized like this, doesn matter..
thanks for replies sohguanh and Zhuge,
Ill try it with pointers :(

EDIT:
compilation errors..
error C2059: syntax error : '{'
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Last edited on
It can be initialized but just that for those default constructor you did not assign explicit value to them e.g SpellStats(){ trained=false; } //this constructor only initialized trained and all others will be garbage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

class SpellStats{
        private:
                int encost, cooldown, time;
                bool trained;
        public:
                SpellStats(){ trained=false; }
                SpellStats(bool NT, int EN, int CD, int NTime){
                        trained=NT; encost=EN; cooldown=CD; time= NTime;
                }
        friend ostream& operator<<(ostream& os, const SpellStats &o) {
          os << o.encost << "\n";
          return os;
        }

};

int main() {

SpellStats spell[10]={SpellStats(true, 5,  25, 8),
                      SpellStats(true, 50, 70, 7),
                      SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats(), SpellStats()
                      };

  for(int i=0; i<10; i++) cout << spell[i];

  return 0;
}

thx sohguanh...

but as I though it is only causing errors in vc++ in dev cpp it is not and its working fine
**** the vc++
Topic archived. No new replies allowed.