0

Recently this declaration thing is confusing me. There are two different files, one is score.cpp and the other iscurve1.cpp. The second class Curve1 is inherited from the first class Score.

In score.cpp, I have declared:

ipScore = new int[getSize()];.

This is compiling fine and working without any issues. But when in curve1.cpp, I declare another variable:

new_ipScore = new int[getSize()];

It's saying that 'new_ipScore' : undeclared identifier and

'=' : cannot convert from 'int *' to 'int'

What's the problem here? Anyone can help?

4
  • Did you declare new_ipScore? Commented Apr 23, 2014 at 8:22
  • 1
    Isn't new_ipScore = new int[getSize()]; the declaration? If it's not, then why ipScore = new int[getSize()]; is working without any errors? Thanks Commented Apr 23, 2014 at 8:26
  • ipScore is probably declared. No, new_ipScore = new int[getSize()]; isn't a declaration, it's an assignment to an already declared variable. int* new_ipScore = ... would be a declaration + initialization. Commented Apr 23, 2014 at 8:27
  • 1
    Stuuuuupid MEEEEEEE. Yes it was already declared in private of class Score Haha thanks any way ;) huff Commented Apr 23, 2014 at 8:28

3 Answers 3

2

Why are you asking us if the compiler has already said what is the problem?!

'new_ipScore' : undeclared identifier

Where is this identifier declared? We do not see what the type of the identifier is. Maybe you can say this yourself?

Sign up to request clarification or add additional context in comments.

2 Comments

Yes Mr. Vlad, ipScore was already declared in private of class Score while new_ipScore was not. Stupid me..
@Silver Falcon We could only repeat what the compiler already said adding some words.
2
new_ipScore = new int[getSize()];

It's saying that 'new_ipScore' : undeclared identifier and

'=' : cannot convert from 'int *' to 'int'

With new int[...] you dynamically allocate an array of integers on the heap. The return value is a pointer to the first item in this array, i.e. int*.

Probably you haven't defined the new_ipScore variable in your code, so the compiler has no idea about that; in fact, the compiler is complaining: 'new_ipScore' : undeclared identifier.

Just define new_ipScore as an int*.

Note also that in modern C++, the usual way of managing arrays is by using std::vector, in your case std::vector<int>.

2 Comments

Thanks man. My stupidity that ipScore was already declared in private of class Score. Thanks anyway.
@SilverFalcon: No problem. Everyone of us has done (and will do) wrong things in code: they are called bugs :)
1

Your new_ipScore is not declared. Please declare it. I have noticed that you have followed object-oriented paradigm. So maybe ipScore is declared in private of a class? A wild guess would be Score class. So please check before posting.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.