矢量结构和自定义功能起作用

时间:2011-04-30 16:33:43

标签: c++ eclipse g++ segmentation-fault

我正在尝试为我的Geometry类创建一个C ++项目。我希望用户能够存储和访问变量。为此,我创建了一个struct var,其中包含string namefloat value。我有一个vector < var > varList来保存变量。但是,在编译时,程序根本不能正常工作......首先,它检查变量“dog”是否存在,它显然不存在,并发现它确实存在。然后它尝试更改变量dog并changeVar,而不是返回ERR_NONEXISTENT,返回正确的退出状态为零。在检查变量时,它看到它不存在。然后,在尝试列出所有变量时,会产生分段错误。见下文:

Building Generator 1.0 Alpha
   Variable Systems Test   
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter a variable name:
dog
Enter a value (A FLOAT!):
2.2
Checking to see if dog exists.
It exists!
Changing variable. Function returned 0
Enter a variable to check:
dog
Variable "dog" doesn't exist!
Segmentation fault

我的来源是here。我正在使用Eclipse Helios编译,使用G ++ 4.2.1,在Mac 10.6.7 Snow Leopard上编译。发生了什么事?

如果这不起作用,我会尝试找出std::map ...

另外,这只是我的第二个问题;请原谅(但通知我)任何格式错误。

谢谢,

BF

编辑:这是一些代码:

vsystem.cpp

/*
 * vsystem.cpp
 *
 *  Created on: Apr 29, 2011
 *      Author: wjc
 */


#include <string>
#include <vector>
#include <sstream>
#include "vsystem.h"

using namespace std;

vector <var> varList;

int addVar(string varName, float value){
    // Check to see if varName already exists
    bool varExists = false;
    for (unsigned int i=0; i<varList.size(); i++){
        if (varList[i].name == varName){
            varExists = true;
            return ERR_VAR_EXISTS;
        }
    }

    // Good! The variable doesn't exist yet.
    var tempVar;
    tempVar.name = varName;
    tempVar.value = value;
    varList.push_back(tempVar);
    return 0;
}

int changeVar(string varName, float newValue){
    // Check to see if varName exists
    for(unsigned int i=0; i<varList.size(); i++){
        if(varList[i].name != varName){    // If it doesn't match…
            if (i == varList.size() - 1) // And it's the last one…
                return ERR_NONEXISTENT;  // Uh oh!
        } else { // Found it!
            varList[i].value = newValue;
        }
    }
    return 0;
}

fetchResult fetchVar(string varName){
    fetchResult returnValue;
    // Check to see if varName exists
    for(unsigned int i=0; i<varList.size(); i++){
        if(varList[i].name != varName){     // If it doesn't match…
            if (i == varList.size() - 1){ // And it's the last one…
                returnValue.good = false; // Uh oh!
                returnValue.result = -1;
            } else {
                returnValue.good = true;
                returnValue.result = varList[i].value;
            }
        }
    }
    return returnValue;
}

bool checkVar(string varName){
    // Check to see if varName exists
    for(unsigned int i=0; i<varList.size(); i++){
        if(varList[i].name != varName){  // If it doesn't match…
            if (i == varList.size() - 1) // And it's the last one…
                return false;            // It's not here.
        }else break;
    }
    return true;
}

vector < var > getVarList(){
    return varList;
}


string getVarList(string varDelim, string valueDelim){
    stringstream final;
    for (unsigned int i=0; i<varList.size()-1; i++){
        final<<varList[i].name<<valueDelim<<varList[i].value<<varDelim;
        // add variable name, delim 1 (probably tab), variable value, delim 2 (probably newline)
    }
    final<<varList.back().name<<valueDelim<<varList.back().value;
    // same, but don't add a newline (or other)
    return final.str();
}

vsystem.h

/*
 * vsystem.h
 *
 *  Created on: Apr 29, 2011
 *      Author: wjc
 */


#include <vector>
#include <string>
#include "consts.h"

using namespace std;

#ifndef VSYSTEM_H_
#define VSYSTEM_H_

struct fetchResult {
    float result;
    bool good;
};

struct var {
    string name;
    float value;
};

int addVar(string varName, float value);
int changeVar(string varName, float newValue);
fetchResult fetchVar(string varName);
bool checkVar (string varName);

vector < var > getVarList();
string getVarList(string varDelim, string valueDelim);

#endif /* VSYSTEM_H_ */

ui.h

/*
 * ui.cpp
 *
 *  Created on: Apr 26, 2011
 *      Author: wjc
 */

#include <iostream>
#include <vector>

#include "filedaemon.h"
#include "vsystem.h"

using namespace std;

int runUI(){
    cout << "   Variable Systems Test   "<<endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout << endl;
    cout<<"Enter a variable name:"<<endl;
    string varname;
    cin>>varname;
    cout<<"Enter a value (A FLOAT!):"<<endl;
    float value;
    cin>>value;
    cout<<"Checking to see if "<<varname<<" exists."<<endl;
    bool alreadythere = checkVar(varname);
    alreadythere ? cout<<"It exists!"<<endl : cout<<"It doesn't exist."<<endl;
    if (alreadythere){
        cout<<"Changing variable. Function returned "<<changeVar(varname, value)<<endl;
    } else {
        cout<<"Setting variable. Function returned "<<addVar(varname, value)<<endl;
    }
    cout<<"Enter a variable to check:"<<endl;
    string varcheck;
    cin>>varcheck;
    fetchResult result = fetchVar(varcheck);
    if(! result.good){
        cout<<"Variable \""<<varcheck<<"\" doesn't exist!"<<endl;
    } else {
        cout<<"Variable \""<<varcheck<<"\" is equal to "<<result.result<<endl;
    }
    cout<<getVarList("\n","\t")<<endl;
    string exitstr;
    cin>>exitstr;
    return 0;
}

main.cpp只调用runUI()

1 个答案:

答案 0 :(得分:0)

你在向量上循环并返回true / false的方式很奇怪,你的应用程序因checkVar()而崩溃。

我建议您将varList上搜索项目的所有循环更改为更简单易读的内容,例如:

bool checkVar(string varName)
{
    // Check to see if varName exists
    for (unsigned int i=0; i<varList.size(); i++)
    {
        if (varList[i].name == varName)
        {   // If matches,
            return true;        
        }   
    }   

    // If execution reaches here, it means it didn't found a match
    return false;
}

这解决了崩溃问题。我不知道您的应用程序是否有任何其他错误,但这是我目前的输出:

Building Generator 1.0 Alpha
   Variable Systems Test   
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter a variable name:
dog
Enter a value (A FLOAT!):
2.2
Setting variable. Function returned 0
Enter a variable to check:
alpha
Variable "alpha" doesn't exist!
dog     2.2

修改

另一个问题是你的定义:ERR_NONEXISTENT和ERR_VAR_EXISTS都是1。他们应该有不同的价值观!我正在粘贴下面的相关代码:

<强> consts.h

#ifndef CONSTS_H_
#define CONSTS_H_

#define VERSION "1.0 Alpha"

// Variable errors
#define ERR_NONEXISTENT 0
#define ERR_VAR_EXISTS 1

// File r/w errors
#define ERR_FILE_OPEN 2

#endif /* CONSTS_H_ */

<强> vsystem.cpp

#include <string>
#include <vector>
#include <sstream>
#include "vsystem.h"

using namespace std;

vector <var> varList;

int addVar(string varName, float value){
        // Check to see if varName already exists
        bool varExists = false;
        for (unsigned int i=0; i<varList.size(); i++){
                if (varList[i].name == varName){
                        varExists = true;
                        return ERR_VAR_EXISTS;
                }
        }

        // Good! The variable doesn't exist yet.
        var tempVar;
        tempVar.name = varName;
        tempVar.value = value;
        varList.push_back(tempVar);
        return ERR_NONEXISTENT;
}

int changeVar(string varName, float newValue){
        // Check to see if varName exists
        for(unsigned int i=0; i<varList.size(); i++){
                if(varList[i].name == varName)
        {   // If it match, replace the value
                        varList[i].value = newValue;
            return ERR_VAR_EXISTS;
                }
        }

    return ERR_NONEXISTENT;  // Uh oh!
}

fetchResult fetchVar(string varName){
        fetchResult returnValue;
        // Check to see if varName exists
        for(unsigned int i=0; i<varList.size(); i++){
                if(varList[i].name == varName){     // If it matches
                        if (i == varList.size() - 1)
            {
                                returnValue.good = true;
                                returnValue.result = varList[i].value;
                    return returnValue;
                        }
                }
        }

    returnValue.good = false; // Uh oh!
        returnValue.result = -1;
        return returnValue;
}

bool checkVar(string varName)
{
        // Check to see if varName exists
        for(unsigned int i=0; i<varList.size(); i++){
                if(varList[i].name == varName)
        {   // If matches, return true
                    return true;
                }
        }

    // If execution reaches here, it means it didn't found a match
        return false;
}

vector < var > getVarList(){
        return varList;
}


string getVarList(string varDelim, string valueDelim){
        stringstream final;
        for (unsigned int i=0; i<varList.size()-1; i++){
                final<<varList[i].name<<valueDelim<<varList[i].value<<varDelim;
                // add variable name, delim 1 (probably tab), variable value, delim 2 (probably newline)
        }
        final<<varList.back().name<<valueDelim<<varList.back().value;
        // same, but don't add a newline (or other)
        return final.str();
}