I'm trying to write a Qt program in eclipse with C++, but I can't get past an error:
void MyTests::populateFirstList(){
Question* q = new Question;
q = this->ctr->getCurrent();
string s = this->ctr->toString(q);
}
Question is a type defined by me, and the line with toString(q) returns an error saying invalid arguments.
The funcion toString():
string Controller::toString(Question* q){
string s="";
string text = q->getText();
char c;
string::iterator it;
for (it= text.begin(); it != text.end(); it++)
{
if ((*it) == ' ') {
s+="\n";
}
else {
s+=it;
}
}
return s;
}
And just to be safe, the function getCurrent():
Question* Controller::getCurrent(){
return this->question;
}
I don't understand why this happens, because the function toString() should take a pointer to a Question, and q is one. I'm not even sure if the error is caused within these functions or somewhere deeper. Thanks for any help.
The error message is:
invalid arguments ' Candidates are:
std::basic_string < char,std::char_traits < char >, std::allocator < char > >
toString(Question *) '
Perform : Run QMake
then build
:)
Eventually your error comes from line
} else s+=it;
which eventually should be
} else s+=*it;
This will probably not solve the question, but i do not see any QT in here. Why not make use of QT-Objects when writing a Qt-App?
void MyTests::populateFirstList(){
Question* q = this->ctr->getCurrent();
QString s = this->ctr->toString(q);
}
QString Controller::toString(Question* q){
QString s;
QString text = q->getText();
s = text.replace(" ","\\n");
return s;
}
Question* Controller::getCurrent(){
return this->question;
}
Related
I am using the latest version of Code::Blocks. I have a function that passes in a string and a vector. The function compiles with no errors. However, when I run the debugger, it immediately leads me to line 118 (which I have noted) and gives me trouble. The error that comes up says "Cannot find bounds of current function".
Here is the function, which takes in a line of code of a variable declaration (like "var c=0"), and gets the variable of it and adds its value to the vector, v, a struct with an int value and string name:
char get_variable_declaration(string line, vector<variable> &v)
{
string b;
variable t;
char d[0];
int counter = 0;
int a;
for (int i = 0; i<line.size(); i++) {
if (line[i] == 'r' && counter != 1) {
b[0] = line [i+2];
counter ++;
}
if (line[i] == '=') {
b[1]=line[i+1];
}
}
t.name = b[0];
d[0] = b[1];
a = atoi (d);
t.value = a;
v.push_back (t);
return b[0];
//This function will take in a line of code
//that is confirmed to have a variable declaration
//it will add the variable to the list of
//vectors
}
Here is when it is called:
bool read_code(string file_name, vector<funct> &my_functions, vector<variable> & v)
{
vector<string> code;
string s;
std::size_t found;
bool flag;
funct new_function;
ifstream in;
in.open(file_name.c_str());
if(in.is_open())
{
//read in file line by line and put it into a vector called code
while(in.peek()!=EOF)
{
getline(in,s);
code.push_back(s);
}
in.clear();
in.close();
//read through each line of the code, determine if it's a variable or function (definition or call)
//here it makes reference to functions (listed following this one) which will actually decompose the line
//for information
for(int i=0;i<code.size();i++)
{
//check if it's a variable declaration
found = code[i].find("var");
if(found!=std::string::npos) //its a variable declaration
get_variable_declaration(code[i], v); //ERROR CANNOT FIND..
//check if it's a function. it'll go in the list of functions
found = code[i].find("funct");
if (found!=std::string::npos) //that means it's a function
{
new_function.funct_name=get_function_name(code[i]);
new_function.commands.clear();
i+=2; //skip over the open curly brace
flag=false;
while(!flag)
{
found = code[i].find("}");
if(found==std::string::npos)
{
new_function.commands.push_back(code[i]);
i++;
}
else
{
my_functions.push_back(new_function);
flag=true;
}
}
}
}
return true;
}
else
{
cout << "Cannot locate this file" << endl;
return false;
}
}
Disclaimer: Yes, this is a homework assignment. No, I am not looking for anyone to finish this assignment for me. But, I am still mostly a novice at coding, in need of some assistance, so I ask if you know what is going on, please help me address this issue. Thanks!
Edit: I have gotten this to work on another compiler w/o the text file I am reading from. Not sure if this is a universal issue, or one that the other compiler just didn't pick up on.
Multiple problems with this section of code:
string b;
for (int i = 0; i<line.size(); i++) {
if (line[i] == 'r' && counter != 1) {
b[0] = line [i+2];
counter ++;
}
if (line[i] == '=') {
b[1]=line[i+1];
}
}
Problems:
If the last character in line is 'r', undefined behavior can occur.
If the next to last character in line is 'r', undefined behavior can occur.
If the last character in line is '=', undefined behavior occurs.
Both assignments to b[0] and b[1] is undefined behavior. The b string is empty.
There are also other instances of undefined behavior that have been noted in the comments, which I won't duplicate.
I found the problem. To correctly use atoi, you cannot use a specific character from a string or character array. If you declare a char a[3], and you want to use atoi, you must use it like int value = atoi(a) and not value = atoi(a[2]). If you do not do it this way, it will cause a runtime error.
My question is this:
Does the error appear exactly where the void value is not being ignored or can it appear at a function call that had this error occur internally?
For example, the specific problem I am having...
In my main.cpp, I have the following declarations and function call:
Dictionary * D = new Dictionary(&dictionaryFile, dictionaryName);
ifstream checkFile;
...
*D->spellCheck(&checkFile, fileName, &cout);
The function call here gives the error:
"error: void value not ignored as it ought to be"
Is this specific function call trying to use a void value or could it be within the function, which is defined as follows, in Dictionary.cpp:
void Dictionary::spellCheck(ifstream * checkFile, string fileName, std::ostream * out){
_report.setFileName(fileName);
string end = "\n";
int words = 0;
int wrong = 0;
string word;
char currChar;
while(*checkFile >> word){
currChar = checkFile->get();
while(currChar != ' ' && currChar != ',' && currChar != '.'){
word += currChar;
currChar = checkFile->get();
}
*out << word << end;
word.clear();
}
/*
_report.setWordsRead(words);
_report.setWordsWrong(wrong);
_report.printReport();
*/
}
Many thanks in advance!
*D->spellCheck(...) first calls D->spellCheck, then tries to dereference its return value. Since it returns void, you can't dereference the return value.
Remove the *.
So, basically I'm creating a program that at certain points needs to display text one character at a time with intervals between each character. I created a function that I can pass a string into that is supposed to display the string one character at a time slowly. The only problem is when I'm taking each character from the string, I get the error -> "terminate called after throwing an instance of 'std::out_of_range'what(): basic_string::at"
I've been trying to find the problem for quite a while and all the other code seems to be working, but code that puts the character from the string into the array of characters I made, and I have no idea how to fix it. Any help is much appreciated.
Code:
std::string SlowText(std::string s)
{
int L = s.length();
char *Text;
Text = new char[L];
int c = L;
while(c > 0)
{
Text[c] = s.at(c);
--c;
}
c = L;
while(c > 0)
{
std::cout << Text[c];
Sleep(250);
--c;
}
return "";
}
The reason is that L is the length of the array and you do this:
c = L;
because of 0 indexing you are starting past the end of the string. Try this:
c = L-1;
Of course since this is c++, I am going to tell you the standard thing which is don't use arrays! your code could be this:
std::string SlowText(std::string s)
{
for (auto b = s.rend(), e = s.rbegin(); b != e; b++)
{
std::cout << *b << std::flush;
sleep(250)
}
return ""; //Also why have this?
}
I am attempting to write a simple login form. I want to store the list of login accounts in a STL map.
when retrieving text values from text boxes on the form. The boxes return "String^"
So what I have is:
map <String^, String^> NamePassList;
typedef pair<String^, String^> StringPair;
string line, usrName, password;
usrName = password = "";
ifstream ifs("login.in");
if (ifs.is_open()){
while (!ifs.eof()){
getline(ifs,line);
bool endofusername = false;
for (int i = 0; i < line.length(); i++){
if (line[i] == ' '){
endofusername = true;
}
if (!endofusername){
usrName += line[i];
}
else{
password += line[i];
}
}
String ^ temp1 = gcnew String(usrName.c_str());
String ^ temp2 = gcnew String(password.c_str());
NamePassList.insert(StringPair(temp1, temp2));
}
ifs.close();
}
String ^ UserName = txtUserName->Text;
String ^ Password = txtPassword->Text;
map<String^, String^>::iterator nameItor;
nameItor = NamePassList.find(UserName);
if (nameItor->first == UserName){
if (nameItor->second == Password){
MessageBox::Show("Sucsess!", "log", MessageBoxButtons::OK);
}
else
MessageBox::Show("Fail!", "log", MessageBoxButtons::OK);
}
else
{
MessageBox::Show("Fail!", "log", MessageBoxButtons::OK);
}
when compiled the error i get of from the "map class" and utility.
This seams like the best way to do it, but everything I try gives me a new error.
Any help would be great!! thanks all.
To possibly reduce some errors, try and simplify your code using std::string.
Given the option not to use the managed String type, I'd opt for std::string. Since you're using std::string already, try that type for NamePassList. Also, typedef the map. Then for inserts, use value_type.
typedef map<string, string> StringMap;
StringMap NamePassList;
to insert:
StringMap::value_type vt(usrName, password);
StringMap::_Pairib pair = NamePassList->insert(vt);
if (pair.second == false)
{
... problems... key already exists ....
}
And also for the search:
StringMap::iterator iter = NamePassList.find(UserName);
if (iter != NamePassList.end())
{
...found...
}
You don't want to "use" the iter until you know it's valid by testing against end().
Hello I am creating a parser of sorts in C/C++ it is rather simple i just want to be able to get a string from to tags "(" and ")" using C/C++ i know that the logic is like find first tag and increment a number every single char that is found until the next tag is found. But i suck a logic so if someone could at least give me a function that could help.
Edit:I see that C/C++ string functions are nothing alike so just C++ will do.
You seem unsure of the differences between string handling in C and in C++. Your description seems to imply wanting to do it in a C-style way.
void GetTag(const char *str, char *buffer)
{
buffer[0] = '\0';
char *y = &buffer[0];
const char *x = &str[0];
bool copy = false;
while (x != NULL)
{
if (*x == '(')
copy = true;
else if (*x == ')' && copy)
{
*y = '\0';
break;
}
else if (copy)
{
*y = *x;
y++;
}
++x;
}
}
Alternatively, the C++ way is to use the std::string which is safer because it's not fiddling with pointers, and arguably easier to read and understand.
std::string GetTag(const std::string &str)
{
std::string::size_type start = str.find('(');
if (start != str.npos)
{
std::string::size_type end = str.find(')', start + 1);
if (end != str.npos)
{
++start;
std::string::size_type count = end - start;
return str.substr(start, count);
}
}
return "";
}