Error while compiling using c++11

Hello

I want to take the value from a file named "RectCoordinates.txt" and then check the conditions in function- "get_touching_rectangles" and give output. Here is my code-

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
62
63
64
65
66
67
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;


struct Rectangle {
    int x1, y1;
    int x2, y2;
    int x3, y3;
    int x4, y4;
};


bool operator==(const Rectangle& lhs, const Rectangle& rhs) {
    return  lhs.x1 == rhs.x1 && lhs.y1 == rhs.y1 &&
            lhs.x2 == rhs.x2 && lhs.y2 == rhs.y2 &&
            lhs.x3 == rhs.x3 && lhs.y3 == rhs.y3 &&
            lhs.x4 == rhs.x4 && lhs.y4 == rhs.y4;
}


vector<int> get_touching_rectangles(Rectangle base,
        vector<Rectangle> rectangles) {
    vector<Rectangle> ret;


    for (auto it = rectangles.begin(); it != rectangles.end(); it++) {
        Rectangle other = *it;
        if (base == other) {
            continue;
        }

        
        if ((other.x2 >= base.x1 && other.x1 <= base.x2) &&
                (other.y1 == base.y3 || other.y3 == base.y1)) {
            ret.push_back(other);
            continue;
        }

        
        if ((other.y3 >= base.y2 && other.y2 <= base.y3) &&
                (other.x1 == base.x3 || other.x3 == base.x1)) {
            ret.push_back(other);
            continue;
        }

    }

    return ret;
}

int main()
{
vector<Rectangle> rectangles;


ifstream inputFile;
inputFile.open("RectCoordinates.txt");


get_touching_rectangles(rectangles.at(2) /* Rectangle #3 */, rectangles);

 inputFile.close();
    return 0;
}


I compile it using c++11. But it gives me the following error-

g++ -std=c++11 st5.cpp -o ssst5.cpp: In function ‘std::vector<int> get_touching_rectangles(Rectangle, std::vector<Rectangle>)’:
st5.cpp:58:12: error: could not convert ‘ret’ from ‘std::vector<Rectangle>’ to ‘std::vector<int>’


Can anyone help me to fix this?


Either change the return type to match the actual type you're returning or change the type of the thing you're returning to match the return type?
What cire is saying is that on line 24 should be vector<Rectangle> instead of vector<int>. It has nothing to do with c++11
Last edited on
Thanks. I changed line 24 to-

1
2
3
vector<Rectangle> get_touching_rectangles(Rectangle base,
        vector<Rectangle> rectangles) {
    vector<Rectangle> ret;


It remove the error while compiling. But now when I run the program, it gives me error-

terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Aborted


Any help on that?
You open the file, but you don't read the rectangles, therefore rectangles vector is size 0, so it will complain in the main function when you try to call rectangles.at(2)
I have changed my int main function like this-


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

int main()
{


vector<Rectangle> rectangles;

ifstream inputFile;
inputFile.open("RectCoordinates.txt");

if(!inputFile) { // file couldn't be opened
      cout << "Error: file could not be opened" << endl;
      exit(1);
   }

  while ( !inputFile.eof() ) { // keep reading until end-of-file
     get_touching_rectangles(rectangles.at(2) /* Rectangle #3 */, rectangles);
   }
inputFile.close();
    

    return 0;
}


But still get the same error!!!

terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Aborted


What am I doing wrong?
What am I doing wrong?


Still not reading the file.
To restate what was said before, on line 6 you create an empty vector of rectangles. By line 17, you haven't modified it at all, so it is still empty. However, you try to access rectangle number 3, which doesn't exist as the vector is empty.
Well after searching on internet, I rewrite the code like this-

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
int main()
{

vector<Rectangle> rectangles;

ifstream inputFile;


inputFile.open("RectCoordinates.txt");

if(!inputFile.is_open()) { // file couldn't be opened
      cout << "Error: file could not be opened" << endl;
      exit(1);
   }

  if (inputFile) {
                double Coordinages;
                cin<< Coordinages;
        }
     get_touching_rectangles(rectangles.at(2) /* Rectangle #3 */, rectangles);

inputFile.close();


    return 0;
}


It gives me error while compiling-



st5.cpp: In function ‘int main()’:
st5.cpp:78:23: error: no match for ‘operator<<’ in ‘std::cin << Coordinages’
st5.cpp:78:23: note: candidates are:
In file included from /usr/include/c++/4.7/string:54:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from st5.cpp:1:
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:469:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
/usr/include/c++/4.7/ostream:469:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:474:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
/usr/include/c++/4.7/ostream:474:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:480:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
/usr/include/c++/4.7/ostream:480:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<char, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:486:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
/usr/include/c++/4.7/ostream:486:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<char, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:491:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
/usr/include/c++/4.7/ostream:491:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<char, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:511:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
/usr/include/c++/4.7/ostream:511:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’
In file included from /usr/include/c++/4.7/ostream:607:0,
from /usr/include/c++/4.7/iostream:40,
from st5.cpp:1:
/usr/include/c++/4.7/bits/ostream.tcc:323:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
/usr/include/c++/4.7/bits/ostream.tcc:323:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:528:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
/usr/include/c++/4.7/ostream:528:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<char, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:541:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
/usr/include/c++/4.7/ostream:541:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<char, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:546:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
/usr/include/c++/4.7/ostream:546:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<char, _Traits>’
In file included from /usr/include/c++/4.7/iostream:40:0,
from st5.cpp:1:
/usr/include/c++/4.7/ostream:600:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
/usr/include/c++/4.7/ostream:600:5: note: template argument deduction/substitution failed:
st5.cpp:78:23: note: ‘std::istream {aka std::basic_istream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’


I may be making some silly mistake. :( :(

But can't figure that out. Please help.
Take a closer look at line 18.
uppss!! Silly me! lol. Thanks for the help.
First read the whole file, close it, then run get_touching_rectangles
Topic archived. No new replies allowed.