pair 의 first 와 second 에 접근하지 못하고있습니다.

leadha의 이미지

c++ STL 을 사용하여 간단한 수식 parser 를 짜보고 있었습니다.
공백을 다 날리면서 각 element 를 vector 에 저장한 다음에 calc_recursive() 로 넘겼습니다.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <cmath>
#include <stdio.h>
#include <utility>
using namespace std;
 
double calc_recursive(vector<string>& v){
        //first pass
        vector<string>::iterator iter;
        int paren_depth;
        double num;
        char buf[50];
        pair<vector<string>::iterator,vector<string>::iterator> p1();
        for(iter=v.begin();iter!=v.end();iter++){
                p1.first=v.end();
                if(*iter=="("){
                        paren_depth++;
                        if(p1.first!=v.end()) p1.first=iter;
                }
                if(*iter==")"){
                        paren_depth--;
                        if(paren_depth==0){
                        p1.second=iter;
                        num=calc_recursive(vector<string>(p1.first+1,p1.second));
                        sprintf(buf,"%lf",num);
                        paren_depth=p1.second-p1.first; // used as difference
                        p1.first=v.insert(p1.first,string(buf));
                        iter=v.erase(p1.first+1,p1.first+2+paren_depth);
                        }
                }
        }
        //second pass - no parenthesis - for ^
        //third pass - for * /
        //last - for + -

그런데 다음과 같이 페어의 iterator 를 접근하지 못하고있습니다.

premature_multipass_recursive_calc_parser.cpp:19:6: error: request for member ‘first’ in ‘p1’, which is of non-class type ‘std::pair<__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >, __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > > >()’
   p1.first=v.end();
      ^
premature_multipass_recursive_calc_parser.cpp:22:10: error: request for member ‘first’ in ‘p1’, which is of non-class type ‘std::pair<__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >, __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > > >()’
    if(p1.first!=v.end()) p1.first=iter;
          ^

이것저것 바꿔봐도 소용이 없고 뭐가 잘못되었는지 전혀 감이 안잡혀 질문드려봅니다.
뭐가 잘못된 건가요?

전체가 필요하시면 댓글 주시면 바로 올리겠습니다!

kukyakya의 이미지

MyClass a();

위의 문장은 MyClass의 기본생성자를 이용하여 a변수를 생성하는 것이 아니라, MyClass 타입을 리턴하는, 인자가 없는 함수 a를 선언하는 것입니다.

p1 함수를 선언하시고나서 p1.first에 접근하려고 하니 에러가 발생하는거죠. p1뒤의 괄호를 제거하시면 에러가 나지 않을 것 같습니다.

익명 사용자의 이미지

감사합니다.

익명 사용자의 이미지

vector::insert, vector::erase는 반복자를 무효화합니다.

pair<vector<string>::iterator,vector<string>::iterator> 대신 pair<vector<string>::size_type,vector<string>::size_type>을 쓰세요

익명 사용자의 이미지

좋은 조언 감사드립니다!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.