结构和自定义函数的矢量
Vector of structs and custom functions acting up
我正试图为我的几何类创建一个c++项目。我希望用户能够存储和访问变量。为此,我创建了一个包含string name
和float value
的struct var
。我有一个vector < var > varList
来保存变量。然而,在编译时,程序不能很好地运行……完全不能。首先,它检查变量"dog"是否存在(显然不存在),并发现它存在。然后,它尝试更改变量dog和changeVar
,而不是返回ERR_NONEXISTENT
,返回正确的退出状态0。在检查变量时,它发现它不存在。然后,当试图列出所有变量时,它会产生分段错误。见下文:
我的来源在这里。我正在编译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();
}
相关文章:
- 通过 NIF 从C++返回自定义数据结构
- 将自定义函数传递到基抽象类中以延迟执行
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 自定义数据结构的优点是什么?
- 每个类实例的自定义函数 (C++)
- C++,当函子不是一个选项时,我如何编写带有自定义函数调用的模板化 RAII 包装器?
- 具有自定义函数的 C++ 矢量构造函数
- 自定义函数转换错误?
- 模板中的自定义函数C++
- 如何将CString转换为使用WCHAR的Windows API和自定义函数?
- r-工作程序中对自定义函数的未定义引用(C++和RcppParallel)
- 传递自定义函数类型时的'incompatible types in assignment'
- 从状态内的自定义函数(不是操作)提升 MSM 调用process_event?
- 如何使用C 代码调用MATLAB自定义函数
- glibc函数的GCC、-flto、-fno内置和自定义函数实现
- C++ - 自定义矩阵结构
- 具有各种访问方法的自定义数据结构
- 使用boost::bind()填充std::min_element()的自定义函数中的结构
- 结构和自定义函数的矢量