小编the*_*ace的帖子

如何将通过构造函数传递的字符串转换为变量?

我当前的一个项目要求我将一个文件的实际名称发送到一个类中,但我无法弄清楚如何将该字符串转换为变量.换句话说,我需要打开Andrew到变量namePerson类.

main.c中

#include <iostream>
#include <string>
#include "Person.h" 

using namespace std;

int main ()
{
Person p1("Andrew");

}
Run Code Online (Sandbox Code Playgroud)

Person.h

 #include <iostream>

 using namespace std;

 class Person 
 {
 public:
    Person (string name);
    void setName(string name) {string name = name;}
    void printName(string name) {cout << name;} 
private:
    string name; 
  };
Run Code Online (Sandbox Code Playgroud)

c++ constructor

1
推荐指数
1
解决办法
1505
查看次数

为什么我的变量没有初始化?

我只是从c转到C++,我正在尝试构建一个计算器.Int'结果'不会通过数学运算初始化.逻辑是,根据操作'',将有一个不同的值分配给'结果'.这似乎不起作用.

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;

int main ()
{
    int n1, n2;
    char s,r;
    int result = 0;
    cout<< "Enter a calculation? (y/n)"<<endl;
    cin>>r;
    while(r=='y')
    {
        cout <<"Enter the first number"<<endl;
        cin>>n1;
        cout<<"Enter the operator"<<endl;
        cin>>s;
        cout<<"Enter the second number"<<endl;
        cin>>n2;

        if ('s' == '*')
        {
            result = n1*n2;
        }
        if ('s' =='+')
        {
            result = n1+n2;
        }

        if ('s' =='-')
        {
            result = n1-n2;
        }

        if ('s' =='/')
        {
            result = n1/n2;
        }
        cout << result<<endl;
        cout<< …
Run Code Online (Sandbox Code Playgroud)

c++ initialization

-2
推荐指数
1
解决办法
131
查看次数

如何打印非素数?PL/SQL

此代码用于打印1到30之间的非素数.它是如何工作的以及错误在哪里.

BEGIN  
    <<outer>>
    FOR i in 1..30
        <<inner>> 
        for k in 2..i-1 loop 
            if (mod(i, k) = 0) THEN 
                DBMS_OUTPUT.PUT_LINE(i);
                exit inner when (mod(i, k)= 0);
            end if;
        end loop inner
    end loop outer 
end;
Run Code Online (Sandbox Code Playgroud)

database oracle plsql

-3
推荐指数
1
解决办法
1241
查看次数

标签 统计

c++ ×2

constructor ×1

database ×1

initialization ×1

oracle ×1

plsql ×1