-
February 9th, 2011, 12:45 AM
#1
General issue - new to C++
Question, I've had this same general compile error for everything I've tried to program in C++. I teaching myself C++ for all my classes this semester, having only studied Java so far, and I just feel like I'm missing something. Below is an example of something I'm trying to do.
Disregard all of the #include statements as this is a test class. Basically, I want to just split a string into an array delimited by whitespace, yet I keep getting the following compilation errors:
test.cpp: In function ‘int main()’:
test.cpp:32: error: ‘split’ was not declared in this scope
test.cpp: At global scope:
test.cpp:37: error: expected unqualified-id before ‘[’ token
test.cpp:48: error: ‘arguments’ was not declared in this scope
test.cpp:49: error: expected unqualified-id before ‘for’
test.cpp:49: error: expected constructor, destructor, or type conversion before ‘<=’ token
test.cpp:49: error: expected constructor, destructor, or type conversion before ‘++’ token
test.cpp:53: error: expected unqualified-id before ‘return’
test.cpp:54: error: expected declaration before ‘}’ token
jregan@ubuntu:~/Dropbox/code/OS.HW1$
I am most confused by the one that says "‘split’ was not declared in this scope" as it keeps popping up regardless of which class/function that I attempt to compile. What am I doing wrong? (Some help clarifying the others wouldn't hurt either). Am I failing because I am too java-minded?
Thanks in advance.
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <jEshHeader.h>
#include <string>
#include <vector>
using namespace std;
int main (){
string str("Hi my name is unaligned and who are you");
string arguments[MAX_ARGS];
split(arguments, str);
}
string[] split(string arguments[], string str){
string buffer;
stringstream ss(str);
int counter = 0;
while (ss >> buffer)
arguments[counter] = buffer;
counter++;
}
// testing whether the arguments were put into the array
int arrSize = sizeof arguments / sizeof(int);
for (int i = 0; i <= arrSize; i++){
cout << arguments[i];
cout << "\n";
}
return arguments;
}
}
-
February 9th, 2011, 07:09 AM
#2
Re: General issue - new to C++
In C++ functions must be declared before use. Move the definition of 'split' above 'main'
The way you are calling it, 'split' should return 'void'.
It would be better to pass a reference to a vector of strings rather than an array.
Code:
void split(vector<string> &arguments, const string &str){
string buffer;
stringstream ss(str);
while (ss >> buffer)
arguments.push_back(buffer);
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 9th, 2011, 07:24 AM
#3
Re: General issue - new to C++
Better still
Code:
void split(vector<string> &arguments, const string &str)
{
istringstream iss(str);
copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(arguments));
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 9th, 2011, 08:43 AM
#4
Re: General issue - new to C++
This may work too (untested)
void split(vector<string> &arguments, const string &str)
{
istringstream iss(str);
arguments.assign(istream_iterator<string>(iss), istream_iterator<string>());
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|