Why is this program not compiling?!

Hey guys! Does anyone know why this program below is not compiling?


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

pair<int, int> F(const vector<int> &A);


int main()
{
    pair<int, int> range;
    
    int arr[] = {2,11,3,5,13,7,19,17,23};
    vector<int> E(arr, arr+9);
    
    range = F(E);
    cout<<"The range of increasing subarray is: ["<<range.first<<", "<<range.second<<"]"<<endl<<endl;
    
    
    // wait until user is ready before terminating program
    system("PAUSE");
    return 0;
}



pair<int, int> F(const vector<int> &A) 
{
    int max_length = 1; 
    pair<int, int> ans(0, 0);
    int i = 0;
    while(i < A.size())
    {
        //backward check and skip if A[j] >= A[j+1]
        bool is_skippable = false;
        for (int j = i + max_length - 1; j >= i; --j)
        {
            if (j + 1 >= A.size() || A[j] >= A[j + 1]) 
            {
                i = j + 1;
                is_skippable = true;
                break;
            }
        }
        
        //forward check if it is not skippable
        if (is_skippable == false) 
        {
            i += max_length - 1;
            while ((i + 1 < A.size()) && (A[i] < A[i + 1])) 
            {
                ++i;
                ++max_length;
            }
            ans = {i - max_length + 1, i};
        }
    }
    return ans;
}


When I tried to compile it, I got the error message below concerning line 55:

In function `std::pair<int, int> F(const std::vector<int, std::allocator<int> >&)':
55: error: expected primary-expression before '{' token
55: error: expected `;' before '{' token
Execution terminated


I just can't see anything syntactically or semantically wrong with the function (i.e. function F). Does anyone know what is going on here?
Last edited on
In function 'std::pair<int, int> F(const std::vector<int>&)':
32:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
38:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
47:33: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
50:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]


Hi,

size returns a std::size_t type, so consider making the types of variables that compare to it std::size_t as well.

Line 47: use == rather than = in a comparison.

Avoid using namespace std;, just put std:: before each std thing, believe me it's the best way and all the expert coders do it that way.

Good Luck !!
I made the correction about the IF's logical expression operator in line 47 (changing it from 'assignment' to 'relational') and used the C++ shell on this site to run the program. Guess what, it worked!! But for some curious reason, I still can't get it to compile on the Dev C++ compiler I use!!

What could explain this strange difference in behavior?
Guess what, it worked!! But for some curious reason, I still can't get it to compile on the Dev C++ compiler I use!!


There are still the warnings on cpp.sh , these won't prevent the program from running. However warnings are there for a reason, they might be the cause of runtime logical errors for example. It's a good idea to get to the point where the code compiles without any warnings or errors. Some people even use the complier options to turn warnings into errors.

I already explained how to fix the warnings, in my previous post.

TheIdeasMan wrote:
size returns a std::size_t type, so consider making the types of variables that compare to it std::size_t as well.


What could explain this strange difference in behavior?


Did you compile with the same warnings / errors options on? A minimum for g++ would be -Wall -Wextra -pedantic
> What could explain this strange difference in behavior?

The the Dev C++ compiler is too old.

1
2
3
4
5
6
7
std::pair<int, int> ans ;

// ...

ans = {i - max_length + 1, i}; // uniform initialisation: requires a C++11 implementation

ans =  std::pair<int, int>( i - max_length + 1, i ) ; // this would work with legacy C++ 
Thanks a lot @JLBorges! Your suggestion (the second assignment statement) has resolved the problem! Please, I have a few questions:

1. Are you essentially performing a static_cast here? If yes, why is it necessary since all the variables in parentheses were already declared to be int?

2. With a view to removing the warnings flagged off by the C++ shell on this site, I changed the types of the otherwise int variables (in the function) to size_t in conformity to @TheIdealMan's suggestion in earlier posts. Result: the program compiled but it entered an infinite loop when I ran it!! I had to abort it manually.
Do you have an explanation for this?

3. Does the Dev C++ compiler have a C++11 version? If no, what other good, stable, free C++11 editor/compilers are out there?
29
30
31
32
33
34
35
36
37
    int max_length = 1; 
    pair<int, int> ans(0, 0);
    int i = 0;
    while(i < A.size())
    {
        //backward check and skip if A[j] >= A[j+1]
        bool is_skippable = false;
        for (int j = i + max_length - 1; j >= i; --j)
        {


When the for loop starts, it's initial j value is 0, but the increment is --j, so it tries to go negative straight away, but std::size_t is unsigned. Maybe that is a logical error you have there? Line 38 attempts to access A[j] and A[j+1]. If the loop is decrementing, I might have expected to start at the end of the array and work down, so maybe max_length should be the size of the vector A?

3. Does the Dev C++ compiler have a C++11 version? If no, what other good, stable, free C++11 editor/compilers are out there?


I would recommend clang from llvm, which relies on gcc. gcc (g++ for c++) is great compiler on it's own, but clang produces easy to understand compiler messages. Remember, you need both of these for clang to work.


https://gcc.gnu.org/
http://llvm.org/


clang also has been one of the best in terms of being up to date with new C++ standards, often making making new things available before the next standard is out :+) I wouldn't be surprised if they already started on C++17.

C++14 is the current standard, so use that when you compile.

Regards

Edit:

When subtracting 1 from 0 with std::size_t , it wraps around, giving the maximum value - probably 264 -1
Last edited on
> Are you essentially performing a static_cast here?

No. A temporary object (a prvalue) of type std::pair<int, int> initialised with the list i - max_length + 1, i
is created, and then this temporary object is assigned to ans.
(An implementation may apply copy-elision.)


> the program compiled but it entered an infinite loop when I ran it!!
> I had to abort it manually. Do you have an explanation for this?

Perhaps you should post the (modified) code that exhibited this behaviour.


> Does the Dev C++ compiler have a C++11 version?

Orwell Dev-C++ http://orwelldevcpp.blogspot.in/

STL's MinGW with GCC 5.3 and Boost 1.60 http://nuwen.net/mingw.html


> clang from llvm, which relies on gcc.

No it does not (except on linux).
(clang/clang++ is part of the core distribution of the BSDs).
Last edited on
JLBorges wrote:
> Perhaps you should post the (modified) code that exhibited this behaviour.

As a refresher, I wrote:
Result: the program compiled but it entered an infinite loop when I ran it!! I had to abort it manually.
Do you have an explanation for this?


Here it is (I apologize for not posting it much earlier!):

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

pair<int, int> F(const vector<int> &A);


int main()
{
    pair<int, int> range;
    
    int arr[] = {2,11,3,5,13,7,19,17,23};
    vector<int> E(arr, arr+9);
    
    range = F(E);
    cout<<"The range of increasing subarray is: ["<<range.first<<", "<<range.second<<"]"<<endl<<endl;
    
    
    // wait until user is ready before terminating program
    system("PAUSE");
    return 0;
}



pair<int, int> F(const vector<int> &A) 
{
    size_t max_length = 1; 
    pair<int, int> ans(0, 0);
    size_t i = 0;
  
    while(i < A.size())
    {
        //backward check and skip if A[j] >= A[j+1]
        bool is_skippable = false;
        for (size_t j = i + max_length - 1; j >= i; --j)
        {
            if (j + 1 >= A.size() || A[j] >= A[j + 1]) 
            {
                i = j + 1;
                is_skippable = true;
                break;
            }
        }
 
     
        //forward check if it is not skippable
        if (is_skippable == false) 
        {
            i += max_length - 1;
            while (i + 1 < A.size() && A[i] < A[i + 1]) 
            {
                ++i;
                ++max_length;
            }
            ans = pair<int, int>(i - max_length + 1, i);
        }
    }
    return ans;
}


Against the backdrop of previous posts, the changes from int to size_t are in lines 29, 31 and 37.
Hi,

I still think this applies:

TheIdeasMan wrote:
When the for loop starts, it's initial j value is 0, but the increment is --j, so it tries to go negative straight away, but std::size_t is unsigned. Maybe that is a logical error you have there? Line 38 attempts to access A[j] and A[j+1]. If the loop is decrementing, I might have expected to start at the end of the array and work down, so maybe max_length should be the size of the vector A?


You can still assign a std::size_t to an int If it's large enough, just don't compare them.

Now is a good time to mention using a debugger, you can see these things for yourself, and it will aid you a great deal in tracking down problems in the future. Hopefully there is a GUI debugger in your IDE.
Topic archived. No new replies allowed.