type 'int' unexpected

A

asit

Please consider the following code

// dequeue.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <deque>
#include <algorithm>

using namespace std;

struct print
{
void operator() (int i)
{
cout<<" "<<i;
}
}myprint;

int _tmain(int argc, _TCHAR* argv[])
{
deque<int> first;
deque<int> second(4, 100);
deque<int> third(second.begin(), second.end());
deque<int> fourth = third;

int arr[] = {32, 5, 332, 78, 8};

deque<int> fifth(arr, arr+(sizeof(arr)/sizeof int)); //Line 28

cout<<"third : ";
for_each(third.begin(), third.end(), myprint);
cout<<endl;

cout<<"fourth : ";
for_each(fourth.begin(), fourth.end(), myprint);
cout<<endl;

cout<<"fifth : ";
for_each(fifth.begin(), fifth.end(), myprint);
cout<<endl;
return 0;
}


When I compile the above code it gives the following error.

Line 28 : type 'int' unexpected

Please tell me how to proceed ??
 
Ö

Öö Tiib

    Please consider the following code

...
        deque<int> fifth(arr, arr+(sizeof(arr)/sizeof int));  //Line 28
...

Line 28 : type 'int' unexpected

Please tell me how to proceed ??

By replacing 'sizeof int' with 'sizeof(*arr)'
 
V

Victor Bazarov

By replacing 'sizeof int' with 'sizeof(*arr)'

The rule is that the *type* has to be in parentheses when using the
'sizeof' operator. An object (or an object expression) doesn't have to be.

While the advice to replace 'sizeof int' with 'sizeof(*arr)' is valuable
(what if you decide to change the type of the array?), it's not *really*
necessary. A solution could simply be to put 'int' in parentheses,
while the parentheses around *arr are superfluous. The expression could be

sizeof arr / sizeof *arr

or

sizeof arr / sizeof(int)

, both are syntactically correct. Their readability is another matter.

V
 
Ö

Öö Tiib

The rule is that the *type* has to be in parentheses when using the
'sizeof' operator.  An object (or an object expression) doesn't have tobe.

While the advice to replace 'sizeof int' with 'sizeof(*arr)' is valuable
(what if you decide to change the type of the array?), it's not *really*
necessary.  A solution could simply be to put 'int' in parentheses,
while the parentheses around *arr are superfluous.  The expression could be

     sizeof arr / sizeof *arr

or

     sizeof arr / sizeof(int)

, both are syntactically correct.  Their readability is another matter.

Thanks, Victor, you explained very well why i suggested *arr instead
of int despite it is not necessary.

The superfluous parentheses in sizeof(*arr) come from current coding
standard i follow. This supposedly produces code where novice team
member does not meet 'sizeof arr' and so does not hopefully attempt to
use 'sizeof int'. Similarly it demands redundant brackets in several
situations.
 
P

Paul

The rule is that the *type* has to be in parentheses when using the
'sizeof' operator. An object (or an object expression) doesn't have to be.

While the advice to replace 'sizeof int' with 'sizeof(*arr)' is valuable
(what if you decide to change the type of the array?), it's not *really*
necessary. A solution could simply be to put 'int' in parentheses,
while the parentheses around *arr are superfluous. The expression could be

sizeof arr / sizeof *arr

or

sizeof arr / sizeof(int)

, both are syntactically correct. Their readability is another matter.

--Thanks, Victor, you explained very well why i suggested *arr instead
--of int despite it is not necessary.

--The superfluous parentheses in sizeof(*arr) come from current coding
--standard i follow. This supposedly produces code where novice team
--member does not meet 'sizeof arr' and so does not hopefully attempt to
--use 'sizeof int'. Similarly it demands redundant brackets in several
--situations.

You mean someone has actually created another C++ standard that differs from
the ISO C++ standard?
 
Ö

Öö Tiib

--Thanks, Victor, you explained very well why i suggested *arr instead
--of int despite it is not necessary.

--The superfluous parentheses in sizeof(*arr) come from current coding
--standard i follow. This supposedly produces code where novice team
--member does not meet 'sizeof arr' and so does not hopefully attempt to
--use 'sizeof int'. Similarly it demands redundant brackets in several
--situations.

You mean someone has actually created another C++ standard that differs from
the ISO C++ standard?

C++ is too flexible toolset as a whole. So a team needs to agree usage
principles when using it together for particular purpose. Since we use
C++ for very various software ... such principles overlap only
partially. There are hundreds of various coding standards for various
purposes accessible from internet.

For example Bjarne Stroustrup together with Kevin Carroll wrote a C++
coding standard for developing embedded systems on some F-35 Joint
Strike Fighter few years ago. This is commonly called "JSF++" so you
can easily find it in Internets.
 
P

Paul

--Thanks, Victor, you explained very well why i suggested *arr instead
--of int despite it is not necessary.

--The superfluous parentheses in sizeof(*arr) come from current coding
--standard i follow. This supposedly produces code where novice team
--member does not meet 'sizeof arr' and so does not hopefully attempt to
--use 'sizeof int'. Similarly it demands redundant brackets in several
--situations.

You mean someone has actually created another C++ standard that differs
from
the ISO C++ standard?

C++ is too flexible toolset as a whole. So a team needs to agree usage
principles when using it together for particular purpose. Since we use
C++ for very various software ... such principles overlap only
partially. There are hundreds of various coding standards for various
purposes accessible from internet.

For example Bjarne Stroustrup together with Kevin Carroll wrote a C++
coding standard for developing embedded systems on some F-35 Joint
Strike Fighter few years ago. This is commonly called "JSF++" so you
can easily find it in Internets.
 
V

Victor Bazarov

--Thanks, Victor, you explained very well why i suggested *arr instead
--of int despite it is not necessary.

--The superfluous parentheses in sizeof(*arr) come from current coding
--standard i follow. This supposedly produces code where novice team
--member does not meet 'sizeof arr' and so does not hopefully attempt to
--use 'sizeof int'. Similarly it demands redundant brackets in several
--situations.

You mean someone has actually created another C++ standard that differs from
the ISO C++ standard?

C++ is too flexible toolset as a whole.[...]

Please don't feed trolls.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top