结构和自定义函数的矢量

Vector of structs and custom functions acting up

本文关键字:自定义函数 结构      更新时间:2023-10-16

我正试图为我的几何类创建一个c++项目。我希望用户能够存储和访问变量。为此,我创建了一个包含string namefloat valuestruct var。我有一个vector < var > varList来保存变量。然而,在编译时,程序不能很好地运行……完全不能。首先,它检查变量"dog"是否存在(显然不存在),并发现它存在。然后,它尝试更改变量dog和changeVar,而不是返回ERR_NONEXISTENT,返回正确的退出状态0。在检查变量时,它发现它不存在。然后,当试图列出所有变量时,它会产生分段错误。见下文:

<>之前建筑生成器1.0 Alpha可变系统测试~~~~~~~~~~~~~~~~~~~~~~~~~~~输入一个变量名:狗输入一个值(一个FLOAT!):2.2检查狗是否存在。它的存在!改变变量。函数返回0输入要检查的变量:狗变量"dog"不存在!段错误之前

我的来源在这里。我正在编译Eclipse Helios,与g++ 4.2.1,在Mac 10.6.7雪豹。发生什么事情了?

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

同样,这只是我的第二个问题;如有格式错误,请原谅(但请通知我)。

谢谢,

男朋友

编辑:这里有一些代码:

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()

你在向量上循环并返回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();
}