如何为一个变量分配另一个类的值
How can you assign a variable a value from another class?
我正在编写一个程序,它需要一个路径名来最终创建一个全局字符串。我目前对这个路径名进行了硬编码,但希望使用全局变量来替换它。我遇到的问题是我的全局变量没有定义。我也在使用JNI,我得到的错误是:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f65f981ddd0, pid=11660, tid=140075985102592
#
# JRE version: 7.0_21-b02
# Java VM: OpenJDK 64-Bit Server VM (23.7-b01 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [libstdc++.so.6+0x9ddd0] std::string::size() const+0x0
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/cbil/Desktop/SIGH_Project/July_9/src/hs_err_pid11660.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
# https://bugs.launchpad.net/ubuntu/+source/openjdk-7/
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
这里有相关的代码,为了简单起见,它被重新格式化:
file1.cpp
#include <jni.h>
#include "file1.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
const char* myPath;
JNIEXPORT jint JNICALL Java_foo_foo(JNIEnv *env, jobject thisobj, jbyteArray passedPath){
/*Some conversion, don't think it is relevant, but it might be*/
jboolean isCopy;
jbyte* path = env->GetByteArrayElements(passedPath,&isCopy);
int length1 = strlen((const char*)path);
char* convertedVar = (char*)malloc(length1+1);
memcpy(convertedVar,path,length1);
convertedVar[length1] = ' ';
convertedVar = (char*)path;
/*End Conversion*/
globalVariable = convertedVar;
... //Some code to use the variable and releases
}
file2.h(这是我声明全局变量的地方)
#include <vector>
#include <fstream>
#include <string.h>
extern const char* globalVariable;
extern int someNum;
extern int someLength;
class file2{
public:
static std::vector<std::string> getSomeString(int &someNum, int &someLength);
private:
static const std::vector<std::string> anotherVar;
...//some other variables and methods
}
最后是调用getSomeString 的代码
file3.cpp
#include "file2.h"
#include <string.h>
#include <vector>
#include <fstream>
using namespace std;
int someNum = 0;
int someLength = 0;
const char* globalVariable;
vector<string> file2::getSomeString(int &someNum, iint &someLength){
vector<string> data;
ifstream infile(globalVariable); //I think the problem is here
string line;
...// some code that goes through the data file specified by the path name
someNum = data.size();
someLength = data[0].size();
return data;
}
const vector<string> file2::anotherVar = getSomeString(someNum,someLength); //variable that uses the getSomeString method
}
我在这里再说一遍,我正在使用JNI。我收到错误,说我创建的lib.so文件有未定义的变量,我不知道这是否是有用的信息。如有任何帮助,我们将不胜感激。很抱歉发了这么长的帖子,我很失落。
char* convertedVar = (char*)malloc(length1+1); memcpy(convertedVar,path,length1); convertedVar[length1] = ' '; convertedVar = (char*)path;
我很确定最后一行不应该在那里。你正在经历这个malloc
/memcpy
的舞蹈,然后迅速将这个分配和初始化的内存块放在地板上,让convertedVar
指向其他东西。
此外,我非常确信一旦函数返回,path
指针将变为无效。
extern const char*globalVariable;
这声明了变量,但没有定义它。您还需要在一个源文件(而不是头文件)中提供一个定义:
const char* globalVariable;
奇怪的是,对于someNum
和someLength
,您已经知道足够多了,但对于globalVariable
却不知道。
相关文章:
- 何时在函数中声明堆栈分配变量?
- 取消引用指针并立即为其分配变量,导致分段错误
- 在C++中动态分配变量类型
- 在函数 (c++) 中分配变量类型和大小数组
- 无法在命名空间范围内分配变量
- 自我分配变量的定义
- 在 CUDA C++中分配变量 - 无法分配设备变量
- 在 CPP 中动态分配变量的数据类型
- 为什么无法在类主体中分配变量?
- const 类方法是否阻止在类外部分配变量?
- 从文件中分配变量,并使用定界符分配
- 分配指针后,是否可以为指针指向的事物分配变量名称?
- 读取外部文件并在类似xml的输入中分配变量
- 有没有办法在不分配变量的情况下将字符串连接到数字
- 如果使用alloca在内联函数中的堆栈上分配变量,那么在内联函数返回后,其引用是否有效
- 堆分配变量的结构成员对齐
- 包含溢出缓冲区的堆栈分配变量,也称为缓冲区
- 在 GetTransfer 之后,我在帐户 1 收到未分配变量错误。我无法弄清楚我搞砸了什么逻辑错误
- 对动态分配的变量的引用与指向相同动态分配变量的指针
- 在C++中为用户输入分配变量