Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在类中对多个函数C++;_C++_Function_Class_Pointers - Fatal编程技术网

C++ 在类中对多个函数C++;

C++ 在类中对多个函数C++;,c++,function,class,pointers,C++,Function,Class,Pointers,所以我有两个函数和一个类。 对于1函数,我想设置存储在类中的整数的值。 对于另一个函数,我想再次使用这些值。 我使用指针,因为我认为它会保存在整个程序的内存地址上 #include <iostream> using namespace std; void Function1(); void Function2(); class TestClass { public: TestClass(); ~TestClass(); void SetValue(int

所以我有两个函数和一个类。 对于1函数,我想设置存储在类中的整数的值。 对于另一个函数,我想再次使用这些值。 我使用指针,因为我认为它会保存在整个程序的内存地址上

#include <iostream>
using namespace std;


void Function1();
void Function2();
class TestClass
{
public:
    TestClass();
    ~TestClass();
    void SetValue(int localValue)
    {
        *value = localvalue;
    }
    int GetValue()const
    {
        return *value;
    }
private:
    *value;
};

TestClass::TestClass()
{
    value = new int(0);
}

TestClass:
~TestClass()
{
    delete value;
}

int main()
{
    TestClass *tommy = new TestClass; //this didn't work,
    //couldn't use SetValue or Getvalue in functions
    Function1();
    Function2();
    return 0;
}

void Function1()
{
    int randomvalue = 2;
    TestClass *tommy = new TestClass; //because it didnt work in main, i've put it here
    tommy->SetValue(randomvalue);
}

void Function2()
{
    TestClass *tommy = new TestClass;
    cout << tommy->GetValue();
            << endl; //this gave a error, so I put the above in    again
    //but this returns 0, so the value isn't changed
}
#包括
使用名称空间std;
void函数1();
无效函数2();
类TestClass
{
公众:
TestClass();
~TestClass();
void SetValue(int localValue)
{
*value=localvalue;
}
int GetValue()常量
{
返回*值;
}
私人:
*价值观;
};
TestClass::TestClass()
{
值=新整数(0);
}
测试类:
~TestClass()
{
删除值;
}
int main()
{
TestClass*tommy=new TestClass;//这不起作用,
//无法在函数中使用SetValue或Getvalue
功能1();
函数2();
返回0;
}
无效函数1()
{
int随机值=2;
TestClass*tommy=newtestclass;//因为它在main中不起作用,所以我把它放在这里
tommy->SetValue(随机值);
}
无效函数2()
{
TestClass*tommy=新的TestClass;
coutgetvalue();

每次编写
newtestclass
,实际上就是在创建一个
TestClass
对象的新实例。新实例与任何现有实例都没有任何关系,只是属于同一类型。要使
TestClass
的单个实例成为“一个”在函数使用时,需要将其作为参数传递给这些函数

此外,除非绝对必要,否则不要使用指针

下面是一个清理过的代码示例,它完成了您尝试的内容

class TestClass
{
   int value;

public:
   TestClass() : value(0)
   {}

   int GetValue() const { return value; }
   void SetValue(int x) { value = x; }
};

// takes a "reference", works somewhat like a pointer but with
// some additional safety guarantees (most likely will not be null)
// This will modify the original passed in TestClass instance.
void SetRandomValue(TestClass& tc)
{
   int random = 2; // not really random...
   tc.SetValue(random);
}

// take a const reference, similar to above comment, but a const
// reference cannot be modified in this scope
void Print(const TestClass& tc)
{
   std::cout << tc.GetValue() << "\n";
}

int main()
{
   // create a TestClass instance with automatic storage duration
   // no need to use a pointer, or dynamic allocation
   TestClass tc;

   // Modify the instance (using reference)
   SetRandomValue(tc);

   // print the instance (using const reference)       
   Print(tc);

   return 0;
}
class测试类
{
int值;
公众:
TestClass():值(0)
{}
int GetValue()常量{返回值;}
void SetValue(int x){value=x;}
};
//获取“引用”,工作方式有点像指针,但
//一些额外的安全保证(很可能不为空)
//这将修改最初传入的TestClass实例。
void SetRandomValue(TestClass和tc)
{
int random=2;//不是真正的随机。。。
tc.设定值(随机);
}
//取一个常量引用,类似于上面的注释,但是是一个常量
//无法在此范围内修改引用
无效打印(常量测试类和tc)
{

std::cout您需要将
tommy
main()
传递到每个函数,而不是每次都创建一个新的,否则您只会丢失在函数中创建的新
Testclass
对象,实际上由于使用
new
而导致内存泄漏

比如:

void Function1(TestClass * tommy) {
    int randomvalue =2;
    tommy->SetValue(randomvalue);
}
然后在
main()
中:


在您的类中,正如其他人指出的那样。

您没有将TestClass传递给任何一个函数,因此它们的函数都看不到您创建的tommy对象。然后在每个函数中创建一个新的局部变量,该局部变量恰好与您的局部变量在main中具有相同的名称……它们都是独立的对象。

请更具体一些.到底什么不起作用?您是否收到错误消息(如果收到,请发布)?您是否收到意外行为(如果收到,请描述)?你听说过缩进吗?让事情变得易懂为什么这被否决了?这是非常有效的好建议。不是我否决了,但这并不能回答为什么他必须写
新测试类
3次。@Agent\L,没错。Edited.ah,当我回答这个问题时,下面有人用代码写了同样的东西。选择他的!@Chad:为什么?问OP,他在问题中说他想使用它们。尽管如此,我并不特别同意指针总是要避免的,但没有看到真正的用例(这意味着我怀疑这是他想要编写的整个程序)我没有办法去做一个关于他们是否在这里最好的看法。我意识到,回答直接的问题也是如此,但是这个问题显然是从C++中没有多少经验的人那里来的。他使用指针是因为他“认为这会保存在内存中”。我们应该努力工作,而不是仅仅做。“代码有效”,但代码“更好”。@Chad:在我编辑时,我怀疑他所发布的内容是否意在代表其程序的完整功能。在不知道他想做什么的情况下,最好不要试图预先判断什么会“更好”",这里。当然,在某些地方,指针是理想的解决方案,据我们所知,也许他想写的就是其中之一。我们确实告诉人们发布他们能做的最短的可编译示例来演示问题。@user2901207:不用谢。Chad的建议不错,在模式中,不合理地使用指针是可以避免的rn C++,虽然我不认为应该采取有力的步骤来避免它们。很明显,我对编程很陌生,当然这部分只是指出我的问题而不是我的整个程序。我之所以使用指针是因为我已经知道指针最终使程序运行得更快,因为你可以删除它们。再说一次,我只是想知道如何使用它们,所以在这个程序中(我制作这个程序是为了提高我的技能),我选择使用指针
int main() {
    TestClass *tommy = new TestClass; 
    Function1(tommy);
    std::cout << tommy->GetValue() << std::endl;  //  Outputs 2
    delete tommy;
    return 0;
}
int main() {
    TestClass *tommy = new TestClass; 
    tommy->SetValue(2);
    std::cout << tommy->GetValue() << std::endl;  //  Outputs 2
    delete tommy;
    return 0;
}
private:
*value;