how to pass a string as an argument?

This is a discussion on how to pass a string as an argument? within the C++ Programming forums, part of the General Programming Boards category; for some reason c++ has a problem when i try to pass a string in as an argument to my ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    how to pass a string as an argument?

    for some reason c++ has a problem when i try to pass a string in as an argument to my method parseLine...

    did i do it correctly ...thanks for any help.

    parser.cpp---
    Code:
    #include <iostream>
    #include <fstream>
    #include <string.h>
    #include "parser.h"
    
    using namespace std;
    
    /* constructor */
    parser::parser(void){
    }
    
    /* reads in the specified file, line by line */
    int parser::readFile(void) {
    
    	string filename="data.txt";
    	string line;
       
    	ifstream file(filename.c_str());
       
    	if(!file.is_open()) {
    
    		cout << "Error...file is not opened" << endl;
            return 1;
    	}	
    
    	while (!file.eof()) {
    		
    		/* get line by line */
    		getline(file, line);
    
    		/* now parse the line */
    		parseLine(line);		
    	}
    
    	file.close();
    
    	return 0;
    }
    
    void parser::parseLine(string line) {
    	
    	cout << "Parsing line: << line << endl;
    }
    parser.h---
    Code:
    #include <iostream>
    #include <string>
    
    class parser {
    	
    public:
    	parser(void);
    	int readFile(void);
    	void parseLine(string line);
    };

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for some reason c++ has a problem when i try to pass a string in as an argument to my method
    I gave away my crystal ball a long time ago. You'll need to be more specific about what "problem" C++ has.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by waxydock
    for some reason c++ has a problem when i try to pass a string in as an argument to my method parseLine...

    did i do it correctly ...thanks for any help.
    Put this in your parser.h:

    Code:
    using std::string;
    (You also missed a quote mark in the cpp file, but you probably already knew that.)

    Code:
    	cout << "Parsing line: "<< line << endl;
    Regards,

    Dave

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    void parser::parseLine(string line) {
    	
    	cout << "Parsing line:" << line << endl;
    }
    You should pass a pointer rather then the class instance itself.
    Code:
    void parser::parseLine(string *line) {
    cout << "Parsing line:" << *line << endl;
    And to call the function
    Code:
    parseLine(&line);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 06:18 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

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