C++在函数中重用 .h 文件中的相同变量名(this.variable = 变量错误?)

发布于 2024-10-10 05:15:34 字数 395 浏览 1 评论 0原文

我的 .h 文件中有一个变量的名称,该函数具有相同的名称... 例如:

//computer.h  
class Computer{  
private:  
    string computerName;  
    int cores;  
}  

//computer.cpp  
Computer::Computer(string computerName, int cores)  
{  
    ...  
}  

我想将 .cpp 函数中的值分配给 .h 文件变量。

每当我这样做时,它都不起作用。我做得正确吗?

this.computerName = computerName;  
this.cores=cores;

I have a name of a variable in my .h file which a function has the same name...
for example:

//computer.h  
class Computer{  
private:  
    string computerName;  
    int cores;  
}  

//computer.cpp  
Computer::Computer(string computerName, int cores)  
{  
    ...  
}  

I want to assign the values from the .cpp function to the .h file variables.

whenever i do this, it doesn't work. Am I doing it correctly??

this.computerName = computerName;  
this.cores=cores;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

演出会有结束 2024-10-17 05:15:34

在 C++ 中,this 是一个指针,因此它是:

this->computerName = computerName;
this->cores = cores; 

In C++, this is a pointer, so it would be:

this->computerName = computerName;
this->cores = cores; 
陌路终见情 2024-10-17 05:15:34

使用初始化代替赋值

Computer::Computer(string computerName, int cores) 
  :computerName(computerName), cores(cores) 
{  
    ...  
}  

是可行的。构造函数初始值设定项列表具有确切的目的。

Instead of assignment, use initialization

Computer::Computer(string computerName, int cores) 
  :computerName(computerName), cores(cores) 
{  
    ...  
}  

Yes that works. Constructor initializer lists have that exact purpose.

微暖i 2024-10-17 05:15:34

如果您发布了实际的错误消息而不是仅仅说“它不起作用”,那么很明显该问题与变量名称无关!

this 是一个指针,因此您应该使用 -> 运算符,而不是 .

this->computerName = computerName;
this->cores = cores;

此外,建议通过引用 const 而不是通过值来接受字符串,并且您应该更喜欢初始化而不是赋值;稍后你会发现,对于某些事情(特别是常量),你实际上必须这样做:

class Computer {
private:
    std::string computerName;
    int cores;
}

Computer::Computer(const std::string& computerName, int cores)
   : computerName(computerName)
   , cores(cores)
{}

If you'd posted the actual error message rather than just saying "it doesn't work", it would become clear that the issue has nothing to do with the variable names!

this is a pointer, so you should use the -> operator, not ..

this->computerName = computerName;
this->cores = cores;

Additionally, it's advisable to accept strings by reference-to-const rather than by value, and you should really prefer initialisation over assignment; you'll find later on that for some things (notably, constants) you actually have to do this:

class Computer {
private:
    std::string computerName;
    int cores;
}

Computer::Computer(const std::string& computerName, int cores)
   : computerName(computerName)
   , cores(cores)
{}
烟酒忠诚 2024-10-17 05:15:34

您声明同一个变量两次。

只需将其更改为 Computer::Computer(string computerName, unsigned int numCores),我将使用 unsigned int,而不是 uusing 和 int。

在 C++ 中,this->cores == cores

you are declaring the same variable twice.

just change it to Computer::Computer(string computerName, unsigned int numCores), and instead of uusing and int, I would use an unsigned int.

in C++, this->cores == cores

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文