2011-08-09 161 views
0

最近刚刚开始回到编程,并开始做一些练习,但我一直刚开一个错误,应该是简单的解决,但不能似乎解决它......串字符被分配到一个char

#include <iostream> 
#include <string> 
#include <stdlib.h> 

using namespace std; 

int main() 
{ 
int numberInts = 3; 
    int strSize = 0; 
    char interator = 'o'; 

    string source[3]; 
    string strTest ("this is a test"); 

    //source = (string*) malloc (3+1); 
    source[0] = "(a+(b*c))"; //abc*+ 
    source[1] = "((a+b)*(z+x))"; 
    source[2] = "((a+t)*((b+(a+c))^(c+d)))"; 

    for(int i=0;i<numberInts;i++) 
    { 
     strSize = source[i].size(); 
     for(int j = 0; j < strSize; j++) 
     { 
      iterator = strTest[0]; 
      if(source[i][j] == '\(') 
      { 
       cout<<"\("; 
      } 

     } 
     cout << "\n"; 
    } 
    return 0; 
} 

行“iterator = strTest [0];”给我一个缺少模板参数错误,我不能真正弄明白为什么不能我分配到一个char返回一个字符的字符串的位置...

感谢

+1

当错误似乎没有道理时,仔细检查您的变量名称。 ;) – Vache

+0

在标准库中使用非常常用的词避免变量名是明智的,例如,迭代器。你会得到一个更明显的错误信息。 –

+0

不惜一切代价避免使用名称空间标准;编译器会给你一个非常有用的错误消息,但是你的命名空间指令。 –

回答

3

一件事,你拼错iteratorinterator当你宣布它。

+0

哇...只是哇...编程中的第一课记得检查你的变量是同一个名字xD – Santi

+1

我在大学时有一个诵读困难的朋友,他从来没有把错误的'整数'当作'整合者'。 – john

1

拼写错误,你的char变量被称为interator而不是迭代器。

1

切换到Clang。这是错误消息更具体。它实际上捕捉了大多数拼写错误,并提供了它认为可能意味着什么的建议。但是,由于迭代器模板的原因,它可能不会将此视为拼写错误。

你会看到以下为错误:

testclang.cpp:8:5: error: cannot refer to class template 'iterator' 
     without a template argument list 
     iterator = 5; 
    ^
    In file included from testclang.cpp:1: 
    In file included from /usr/include/c++/4.4/iostream:39: 
    In file included from /usr/include/c++/4.4/ostream:39: 
    In file included from /usr/include/c++/4.4/ios:40: 
    In file included from /usr/include/c++/4.4/bits/char_traits.h:40: 
    In file included from /usr/include/c++/4.4/bits/stl_algobase.h:67: 
    /usr/include/c++/4.4/bits/stl_iterator_base_types.h:103:12: note: 
     template is declared here 
     struct iterator 

但是没有`使用命名空间std”,这(testclang.cpp):

int main() 
    { 
     int interator = 3; 
     iterator = 5; 
    } 

当铿锵编译:

clang testclang.cpp 

生产:

testclang.cpp:4:5: error: use of undeclared identifier 'iterator'; did 
          you mean 'interator'? 
    iterator = 5; 
    ^~~~~~~~ 
    interator 
相关问题