C++ STL vector iterator인데 런타임에서 문제가..

nzer0의 이미지

STL 처음 익히고 있는데요

win32 프로젝트에 사용해서
성공적으로 빌드 후에 실행해보니까

런타임에서 에러가 발생하기에
브레이크포인트 걸고 디버깅 해보니까

초심자인 저로써는 난생 처음 접하는 황당한 장면을 목격했습니다

	vector<Vertex> currPoly=polygons.back();
	vector<Vertex>::iterator it;
 
 
	for(it=currPoly.begin();it!=currPoly.end();++it){
		vector<Vertex>::iterator p1;
		vector<Vertex>::iterator n1;
		vector<Vertex>::iterator n2;
 
		if(			it==currPoly.begin()){
			p1=currPoly.end();
			n1=it+1;
			n2=it+2;
		}else if(	it==currPoly.end()-1){
			p1=it-1;
			n1=it+1;
			n2=currPoly.begin();
		}else if(	it==currPoly.end()){
			p1=it-1;
			n1=currPoly.begin();
			n2=currPoly.begin()+1;
		}else{
			p1=it-1;
			n1=it+1;
			n2=it+2;
		}
		int tmp;
		tmp=it->x;
		tmp=p1->x;

제가 첨부한 그림에도 나와있는 코드 일부분인데요
Vertex는 그냥 x,y만 가지고 있는 struct입니다

디버깅을 돌리면 그림처럼 n2와 tmp가 선언이 안된 것 처럼 나옵니다.
실제로 n1,p1에도 쓰레기값이 들어있어서 좀 돌다가 런타임 에러가 나더라구요.

귀찮으시겠지만
도와주세요ㅠ

File attachments: 
첨부파일 크기
Image icon K-75.jpg244.42 KB
Image icon K-74.jpg243.83 KB
nzer0의 이미지

첫번째 else if 는 end()-2
두번째 else if 는 end()-1 로 고쳐야 할 것 같아요

end()가 마지막 다음을 가리키고 있다는 것을 깜박

그래도 여전히 선언이 안되어있는 것은 의문...

kukyakya의 이미지

circular list처럼 쓰시려고 하고 계시고, p1은 1단계 앞, n1, n2는 각각 1단계, 2단계 뒤를 가르키고 싶으신게 맞나요?

stl에는 circular list가 없는 것 같아서 circular_advance()를 구현해봤습니다. iterator traits에 따라 최적화할 여지가 있긴 합니다만 일단 돌아는 가는것 같네요 ㅎㅎ

#include <iostream>
#include <vector>
 
template<class Container, class InputIterator, class Distance>
inline void circular_advance(Container& c, InputIterator& i, Distance n) {
  if( n >= 0 ) {
    while( n-- ) {
      if( ++i == c.end() ) i = c.begin();
    }
  }
  else {
    while( n++ ) {
      if( i == c.begin() ) i = c.end();
      --i;
    }
  }
}
 
int main()
{
  std::vector<int> ints;
  ints.push_back(1);
  ints.push_back(2);
  ints.push_back(3);
  ints.push_back(4);
  ints.push_back(5);
 
  for( std::vector<int>::iterator it=ints.begin(), e=ints.end(); it!=e; ++it ) {
    std::vector<int>::iterator p1,n1,n2;
    p1 = n1 = n2 = it;
    circular_advance(ints, p1, -1);
    circular_advance(ints, n1, 1);
    circular_advance(ints, n2, 2);
 
    std::cout << *p1 << ", " << *it << ", " << *n1 << ", " << *n2 << std::endl;
  }
 
  return 0;
}

~/temp/circular $ ./a.out
5, 1, 2, 3
1, 2, 3, 4
2, 3, 4, 5
3, 4, 5, 1
4, 5, 1, 2
~/temp/circular $ 
nzer0의 이미지

감사합니다

stackoverflow에도 물어봤었는데

변수가 선언이 안되어있는 문제는 release로 빌드해서 그렇다네요..

ㄷㄷ 이런 실수를.. 너무 오랜만에 VS를 써서 설정을 못봤네요ㅠ

근데 release에서도 원래 디버깅이 가능한건가요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.