如何为一个变量分配另一个类的值

How can you assign a variable a value from another class?

本文关键字:分配 变量 另一个 一个      更新时间:2023-10-16

我正在编写一个程序,它需要一个路径名来最终创建一个全局字符串。我目前对这个路径名进行了硬编码,但希望使用全局变量来替换它。我遇到的问题是我的全局变量没有定义。我也在使用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;

奇怪的是,对于someNumsomeLength,您已经知道足够多了,但对于globalVariable却不知道。