[C++] Error C2075?

Pages: 12
So what I'm doing is trying to get the two highest numbers out of a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int size=sizeof(sort)/sizeof(typeid(sort).name());
int Lmax, max;
vector<T> Sort(sort, sort + size);

while(!Sort.empty()){
  size=Sort.size();
  if(Sort.at(0)<Sort.at(1)){ Lmax=0; max=1; }
  else{ Lmax=1; max=0; }
  for(int x=0; x<size; x++){
    if(Sort.at(x)>Sort.at(Lmax)){
      if(Sort.at(x)>=Sort.at(max)){Lmax=max; max=x;}
      else Lmax=x;
    } /*Line 52 (The error line) here*/
  }
} 


This produces the error:
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\xmemory|52|error C2075: 'Target of operator new()' : array initialization needs curly braces


If this bit of code is removed, the error goes away. Any ideas what is causing this?
That needs some major indentation.
The code in question appears to refer to an array that you've created with new. Since I don't see any in that code (which is damn difficult to read because there's no line separation in that damn code), you'll probably need to give us more.
Well, first off, that is exactly how it should be indented.
Second, the object new is not used anywhere in my code.

 
/*Unnecessary code removed*/


That is the entire code. (I attempted to space it out a bit more for you; sorry if it isn't enough.)
Last edited on
By indentation, I mean not stacking things up on the same damn lines. You could condense the entire program into one line if you wanted but it would be crap programming. There's no crime involved with not separating your code blocks into multiple lines; on the contrary it improves legibility by multiple orders of magnitude.
If there's no new in your prog I truly can't put my finger on it.
Okay, thanks for trying.

Anybody else know why?
Okay, I think I may have found the error.
typename T is equal (in my case) to int [14]. Thus:
1
2
3
vector<T> Sort(sort, sort + size);
/*This is like saying:*/
vector<int [14]> Sort(sort, sort + size);


What I need it to say is vector<int>....

So is there a standard C++ way to extract the int from the typeid? Or am I stuck with using a if else if system?
1
2
3
4
template<typename T,unsigned SIZE>
void ssort(T (&sort)[SIZE]){
  // T == int
  // SIZE == 14 

Last edited on
Thank you very much! Works perfectly. (But you already knew that. ;) )

For future readers, this is what you do at the top:
1
2
template <typename T, unsigned SIZE>
void ssort(T(&)[SIZE]);
Just out of curiosity...

sizeof(typeid(sort).name())


That's a piece of code, guy..
Is that your way to figure out, whether your compiler runs on wchar_t or char? *g*

If yes, is this guarantee to work? I mean.. I usually use the UNICODE - define for getting this..

Ciao, Imi.
That doesn't work at all. sizeof does its job at compile time. sizeof("T") is not necessarily sizeof(T), but it will always be sizeof(const char *).
Last edited on
That doesn't work at all. sizeof does its job at compile time. sizeof("T") is not necessarily sizeof(T), but it will always be sizeof(const char *).


Hm.. "sizeof(typeid(int).name())" is allowed to return either const char* or const wchar_t*. I thought he is using this to distinguish between wide-character mode or not.

But then again, it would be both a pointer, so if at all, it would need a dereferrence. Like "sizeof( *typeid(int).name() )" but I tried and for my MSVC it seems always to be "const char*" - no matter whether its set to multi-byte, unicode or unset character encoding..

Was just curious..

Ciao, Imi.
"sizeof(typeid(int).name())" is allowed to return either const char* or const wchar_t*.
Assume you didn't mean to leave sizeof there. name() only returns const char *.
UNICODE only affects Window API functions. More specifically, the size of TCHAR.

sizeof( *typeid(int).name() )
That's guaranteed to evaluate to 1.
Assume you didn't mean to leave sizeof there. name() only returns const char *.

Yea, without the sizeof. Hm.. I thought I read something about name() returning wide characters..

Was probably my imagination. Well, I never really used typid().name() anyway for anything important. Thanks for pointing out.

So if the OP is still listening, the conclusion is, that he should replace the strange sizeof-line (line 1) with:
 
int size=sizeof(sort)/sizeof(char*);


Right? ;)

Ciao, Imi.
The reason for that line is to find out how many elements are in the array.

Assuming we have a 15 element int array:
 
sizeof(array);

That would return 60 since each int takes of 4 bits of memory. However, I'm not interested in how much memory it's taking, I just want how big the array is, thus I then divide it by 4
 
sizeof(array)/sizeof(int)


However, since I don't know that I'm working with an int, I use typeid().name() which returns int, char, double, ett.

So this line:
1
2
3
4
5
int size=sizeof(array)/sizeof(typeid(unknowType).name())
/*Is equivilent to either*/
int size=sizeof(array)/4
/*OR*/
int size=sizeof(array)/sizeof(int)
That simply doesn't work.
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <typeinfo>

class A{
	char a[100];
};

int main(){
	const char *s="Hello";
	std::cout
		<<"Size of A:                "<<sizeof(A)<<std::endl
		<<"Name of A:                "<<typeid(A).name()<<std::endl
		<<"Size of typeid(A).name(): "<<sizeof(typeid(A).name())<<std::endl
		<<"Name of typeid(A).name(): "<<typeid(typeid(A).name()).name()<<std::endl
		<<"Size of const char *:     "<<sizeof(s)<<std::endl
		<<"Name of const char *:     "<<typeid(s).name()<<std::endl;
	return 0;
}
It works in most occasions. But I see your point. Do you have a better suggestion on how I get the size?
It works in most occasions.
It should almost never work.

Do you have a better suggestion on how I get the size?
You don't "get" the size of array parameters. You pass it as a parameter.
It should almost never work.

Rephrase: Works with all arrays.

You don't "get" the size of array parameters. You pass it as a parameter.

I was afraid you'd say that. Thanks!
Works with all arrays.
Could you give an example?
1
2
int array={0,1,2,3,4,5,6,7,8,9};
cout<<sizeof(array)<<" / "<<sizeof(typeid(array).name())<<" = "<<sizeof(array)/sizeof(typeid(array).name())<<endl;
Last edited on
Pages: 12