出现“a nonstatic member reference must be relative to a specific object”问题

这个错误通常在使用类的非静态成员时出现,提示错误信息为“a nonstatic member reference must be relative to a specific object”,意思是“非静态成员引用必须相对于特定对象”。

在 C++ 中,非静态成员变量和非静态成员函数是依赖于类的实例对象的,只能通过对象名来访问。如果没有指定具体对象,编译器就无法确定成员变量或成员函数的地址,从而导致编译错误。

例如,以下代码就会出现这个错误:

class MyClass {
public:
    int value;
};

int main() {
    MyClass obj;
    int num = obj.value;  // 错误:非静态成员变量必须相对于具体的对象
    return 0;
}

在这个例子中,使用了一个 MyClass 类的非静态成员变量 value,但没有指定具体的对象。正确的做法应该是通过对象名来访问:

class MyClass {
public:
    int value;
};

int main() {
    MyClass obj;
    int num = obj.value;  // 正确:使用具体的对象来访问非静态成员变量
    return 0;
}

需要注意的是,在静态成员函数中访问非静态成员变量或函数也会出现这个错误。因为静态成员函数是独立于类的实例对象的,不能访问非静态成员。如果需要访问非静态成员,必须通过对象名来访问。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
// Java crypt example SJ import javax.crypto.*; import javax.crypto.spec.*; import java.io.*; import java.lang.*; import java.util.*; import java.security.*; public class EncryptionExample { protected String calg = "Blowfish"; // AES. DES, Blowfish protected int keyLen = 128; // 128 for AES, Blowfish, 64 for DES public static void main(String[] args) { EncryptionExample s = new EncryptionExample(); // to call nonstatic methods SecretKeySpec key = s.readkey(); String mess = new String("Hello, world!"); byte[] messb = mess.getBytes(); System.out.println("Plain|" + mess +"|=|" + s.bintohex(messb)); byte[] ct = s.encrypt(messb, key); System.out.println("Encry:" + s.bintohex(ct)); byte[] pt = s.decrypt(ct, key); String dmess = new String(pt); System.out.println("Decry|" + dmess +"|=|" + s.bintohex(pt)); } // main() // encrypt message t with key k public byte[] encrypt(byte[] t, SecretKeySpec k) { try { Cipher c = Cipher.getInstance(calg); c.init(Cipher.ENCRYPT_MODE, k); return c.doFinal(t); } catch (Exception e) { System.err.println("Encryption failed: " + e); } return null; } // decrypt message t with key k public byte[] decrypt(byte[] t, SecretKeySpec k) { try { Cipher c = Cipher.getInstance(calg); c.init(Cipher.DECRYPT_MODE, k); return c.doFinal(t); } catch (Exception e) { System.err.println("Decryption failed: " + e); } return null; } // reads key string from user, returns SecretKeySpec public SecretKeySpec readkey() { SecretKeySpec kp = null; String line; byte [] bin = null; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Give the key or passphrase (" + keyLen/4 + " hex digits or long ascii string) \n : "); line = in.readLine(); // check if input is all hex or not boolean ishex = true; for (int i = 0; i < line.length(); i++) if (Character.digit(line.charAt(i), 16) < 0) { ishex = false; break; } // check hex key length if (ishex && line.length() != keyLen/4) System.err.println("Wrong hex ley lenght (" + line.length() + "/" + keyLen/4 + ")"); // make binary key if (ishex) bin = hextobin(line); else bin = asciitobin(line); // make key for crypto algorithm kp = new SecretKeySpec(bin, calg); System.out.println("Key = |" + bintohex(kp.getEncoded()) + "|"); } catch (Exception e) { System.err.println("Key generation failed" + e); } return kp; } // readkey() // make binary out of hex string public byte[] hextobin(String s) { int len = (s.length()+1)/2; byte[] A = new byte[len]; for (int i = 0; i < len; i++) A[i] = Integer.valueOf(s.substring(i*2, i*2+2), 16).byteValue(); return A; } // returns new 128 bit key using MD5 of the string s public byte[] asciitobin(String s) { byte[] A = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); A = md.digest(s.getBytes()); } catch (Exception e) { System.err.println("Digest failed" + e); } return A; } // returns new hex string representation of A public String bintohex(byte[] A) { int len = A.length; StringBuffer sb = new StringBuffer(len*2); for (int i = 0; i < len; i++) { if ((A[i] & 0xFF) < 0x10) sb.append("0"); sb.append(Integer.toHexString(A[i] & 0xFF)); } return sb.toString(); } } // class
第0章 导读(译者的话) 第1章 关于对象(Object Lessons) 加上封装后的布局成本(Layout Costs for Adding Encapsulation) 1.1 C++模式模式(The C++ Object Model) 简单对象模型(A Simple Object Model) 表格驱动对象模型(A Table-driven Object Model) C++对象模型(Th e C++ Object Model) 对象模型如何影响程序(How the Object Model Effects Programs) 1.2 关键词所带来的差异(A Keyword Distinction) 关键词的困扰 策略性正确的struct(The Politically Correct Struct) 1.3 对象的差异(An Object Distinction) 指针的类型(The Type of a Pointer) 加上多态之后(Adding Polymorphism) 第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual Function”的Class “带有一个virual Base class”的Class 总结 2.2 Copy Constructor的建构操作 Default Memberwise Initialization Bitwise Copy Semantics(位逐次拷贝) 不要Bitwise Copy Semantics! 重新设定的指针Virtual Table 处理Virtual Base Class Subobject 2.3 程序转换语意学(Program Transformation Semantics) 明确的初始化操作(Explicit Initialization) 参数的初始化(Argument Initialization) 返回值的初始化(Return Value Initialization) 在使用者层面做优化(Optimization at the user Level) 在编译器层面做优化(Optimization at the Compiler Level) Copy Constructor:要还是不要? 摘要 2.4 成员们的初始化队伍(Member Initialization List) 第3章 Data语意学(The Semantics of Data) 3.1 Data Member的绑定(The Binding of a Data Member) 3.2 Data Member的布局(Data Member Layout) 3.3 Data Member的存取 Static Data Members Nonstatic Data Member 3.4 “继承”与Data Member 只要继承不要多态(Inheritance without Polymorphism) 加上多态(Adding Polymorphism) 多重继承(Multiple Inheritance) 虚拟继承(Virtual Inheritance) 3.5 对象成员的效率(Object Member Efficiency) 3.6 指向Data Members的指针(Pointer to Data Members) “指向Members的指针”的效率问题 第4章 Function语意学(The Semantics of Function) 4.1 Member的各种调用方式 Nonstatic Member Functions(非静态成员函数) Virtual Member Functions(虚拟成员函数) Static Member Functions(静态成员函数) 4.2 Virtual Member Functions(虚拟成员函数) 多重继承下的Virtual Functions 虚拟继承下的Virtual Functions 4.3 函数的效能 4.4 指向Member Functions的指针(Pointer-to-Member Functions) 支持“指向Virtual Member Functions”之指针 在多重继承之下,指向Member Functions的指针 “指向Member Functions之指针”的效率 4.5 Inline Functions 形式对数(Formal Arguments) 局部变量(Local Variables) 第5章 构造、解构、拷贝 语意学(Semantics of Construction,Destruction,and Copy) 纯虚拟函数的存在(Presence of a Pure Virtual Function) 虚拟规格的存在(Presence of a Virtual Specification) 虚拟规格中const的存在 重新考虑class的声明 5.1 无继承情况下的对象构造 抽象数据类型(Abstract Data Type) 为继承做准备 5.2 继承体系下的对象构造 虚拟继承(Virtual Inheritance) 初始化语意学(The Semantics of the vptr Initialization) 5.3 对象复制语意学(Object Copy Semantics) 5.4 对象的功能(Object Efficiency) 5.5 解构语意学(Semantics of Destruction) 第6章 执行期语意学(Runting Semantics) 6.1 对象的构造和解构(Object Construction and Destruction) 全局对象(Global Objects) 局部静态对象(Local Static Objects) 对象数组(Array of Objects) Default Constructors和数组 6.2 new和delete运算符 针对数组的new语意 Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 Template Template的“具现”行为(Template Instantiation) Template的错误报告(Error Reporting within a Template) Template中的名称决议方式(Name Resolution within a Template) Member Function的具现行为(Member Function Instantiation) 7.2 异常处理(Exception Handling) Exception Handling快速检阅 对Exception Handling的支持 7.3 执行期类型识别(Runtime Type Identification,RTTI) Type-Safe Downcast(保证安全的向下转型操作) Type-Safe Dynamic Cast(保证安全的动态转型) References并不是Pointers Typeid运算符 7.4 效率有了,弹性呢? 动态共享函数库(Dynamic Shared Libraries) 共享内存(Shared Memory)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间简史u

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值