2014-12-06 44 views
2

无论我使用getline函数,我在“error:expected primary-expression before”,“token”在我的代码中。 我看了一些在ifstreams上使用getline的人的例子,我想也许我必须在调用函数之前加上“std ::”(不确定会做什么?),但这也不起作用。C++错误“'”标记之前的预期主表达式(在ifstream上使用getline?)

这是我的代码的第一位;我假设我所做的任何错误都是一样的。

#include <stdio.h> 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

struct ListNode{ 
    string user; 
    string salt; 
    string encrypted; 
    string password; 
    ListNode* next; 
}; 


int main(){ 

    /* Initializes singly linked list to hold user data */ 
    ListNode *root; 
    ListNode *conductor; 
    root = new ListNode; 
    conductor = root; 

    string temp; 

    //creates and opens filestream to password file                  
    ifstream psswdFile ("example.txt"); 

    if(psswdFile.is_open()){ 
    //we assume there is at least one line, and initialize the head of             
    //the list                           
    std::getline(psswdFile&, (conductor->user)&, ':'); 
    std::getline(psswdFile&, (conductor->salt)&, ':'); 
    std::getline(psswdFile&, (conductor->encrypted)&); 
+0

'std :: getline(psswdFile&,(conductor-> user)&,':');'你能解释一下你在做什么吗? – PaulMcKenzie 2014-12-06 00:51:38

+0

你不需要'&'在C++中通过引用传递参数。只要写'std :: getline(pswdFile,[...]' – 2014-12-06 00:54:12

回答

1

它看起来像你误会你传递给C++函数参数引用的方式。不同于通常需要您使用&运营商获得的地址指针,即采取引用不需要你做任何事情的功能:只需通过一个可分配的表达式:

std::getline(psswdFile, conductor->user, ':'); 

编译器有你的函数的前置声明,所以它知道通过你引用的任何东西。

+1

真棒,它的工作!谢谢,我想我需要重读那一章! – possiblyATurtle 2014-12-06 00:55:53

相关问题