小编use*_*120的帖子

在主C++中调用成员函数

#include <iostream>

using namespace std;

class MyClass
{
public:
       void printInformation();
};

void MyClass::printInformation()
{
     return;
}

int main()
{

    MyClass::printInformation();

    fgetc( stdin );
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

我该如何调用该printInformation函数main?该错误告诉我,我需要使用类对象来执行此操作.

c++ function

9
推荐指数
3
解决办法
11万
查看次数

在C++中将所有程序输出写入txt文件

我需要将所有程序输出写入文本文件.我相信它是这样做的,

sOutFile << stdout;
Run Code Online (Sandbox Code Playgroud)

其中sOutFile是创建文件的ofstream对象,如下所示:

sOutFile("CreateAFile.txt" ); // CreateAFile.txt is created.
Run Code Online (Sandbox Code Playgroud)

当我将stdout插入sOutFile对象时,我得到一些似乎相似的代码 八进制 [十六进制]代码或我创建的文本文件中的某种地址.

0x77c5fca0
Run Code Online (Sandbox Code Playgroud)

但令我困惑的是,在我的程序中,我多次使用cout.主要是文字陈述.如果我没弄错那就是程序输出.

如果此代码是地址,它是否包含我的所有输出?我可以把它读回到程序中并找到那个方法吗?

如何将我的所有程序输出写入文本文件?

c++ file-io

8
推荐指数
3
解决办法
3万
查看次数

格式化C++控制台输出

我一直在尝试将输出格式化为控制台的时间最长,并且没有发生任何事情.我一直在尝试尽可能多地使用iomanipofstream&out函数.

void list::displayByName(ostream& out) const
{
    node *current_node  = headByName;

    // I have these outside the loop so I don't write it every time.

    out << "Name\t\t" << "\tLocation" << "\tRating " << "Acre" << endl;
    out << "----\t\t" << "\t--------" << "\t------ " << "----" << endl;

    while (current_node)
    {
        out << current_node->item.getName() // Equivalent tabs don't work?
            << current_node->item.getLocation()
            << current_node->item.getAcres()
            << current_node->item.getRating()
            << endl;

        current_node = current_node->nextByName;
    }

    // The equivalent tabs do …
Run Code Online (Sandbox Code Playgroud)

c++ formatting

7
推荐指数
3
解决办法
3万
查看次数

In Place rotation C++ Practice

我有一个正在运行的旋转函数用于我的"items"int数组.下面的代码完成了它,除了我不必要地转移值.我试图实现"就地"轮换.我的意思是ptrs会增加或减少而不是从数组中获取值.我需要以这种方式"提升"这种方法的效率水平.任何建议?

void quack::rotate(int nRotations)
{
 if ( count <= 1 ) return;
 else  // make sure our ptrs are where we want them.
 {
  intFrontPtr = &items[0].myInt;
  intBackPtr  = &items[count-1].myInt;
 }
 for (int temp = 0; nRotations != 0;)
 {
  if ( nRotations > 0 )
  {
     temp = *intFrontPtr;
    *intFrontPtr = *intBackPtr;
    *intBackPtr  = temp; // Connect temps for the rotation
   --intBackPtr; // Move left [...<-] into the array
  }
  else if ( nRotations < 0 ) 
  {
   temp …
Run Code Online (Sandbox Code Playgroud)

c++ arrays rotation

5
推荐指数
3
解决办法
4248
查看次数

使用C++代码检查是否存在txt文件

首先,我要确定我的文件夹目录中有文本文件.我使用visual studio,这是我的源代码编译的地方.

下面的代码应该说明它不起作用的原因.在视觉工作室.

int main( const int argc, const char **argv )
{
    char usrMenuOption;
    const char *cFileName = argv[ 1 ];
    checkName( cFileName );  // supplying the checkName function with contents of argv[1]

    usrMenuOption = getUsrOption();  // calling another function

    fgetc(stdin);
         return 0;
}
ifstream *openInputFile( const char *cFileName )
{
    // this function might be the pronblem.
    ifstream *inFile;
    inFile = new ifstream; 
    inFile->open( cFileName, ios::in );
    return inFile;
}
bool checkName( const char *cFileName )
{
    // …
Run Code Online (Sandbox Code Playgroud)

c++ file visual-studio

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

在数组C++中移位元素

我已经为我的堆栈对象类开发了一个名为"rotate"的方法.我所做的是,如果堆栈包含元素:{0,2,3,4,5,6,7}我需要向前和向后旋转元素.

如果我需要向前旋转2个元素,那么我们将在数组中有{3,4,5,6,7,0,2}.如果我需要向后旋转,或者-3个元素,那么,查看原始数组,{5,6,7,0,2,3,4}

所以我开发的方法运行正常.它只是非常无效的IMO.我想知道我是否可以使用mod运算符包围数组?或者,如果它们是无用的代码,我还没有意识到,等等.

我想我的问题是,我该如何简化这种方法?例如使用较少的代码.:-)

void stack::rotate(int r)
{
    int i = 0;
    while ( r > 0 ) // rotate postively.
    {
        front.n = items[top+1].n;
        for ( int j = 0; j < bottom; j++ )
        {
            items[j] = items[j+1];                                  
        }
        items[count-1].n = front.n;
        r--;
    }

    while ( r < 0 )  // rotate negatively.
    {
        if ( i == top+1 )
        {
            front.n = items[top+1].n;  
            items[top+1].n = items[count-1].n; // switch last with first
        }

        back.n = items[++i].n; // second …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm

3
推荐指数
3
解决办法
3361
查看次数

operator + overload返回导致内存泄漏的对象,C++

我认为问题是当我重载+运算符时返回一个对象.我尝试返回对该对象的引用,但这样做并不能解决内存泄漏问题.我可以评论这两个陈述:

dObj = dObj + dObj2;
Run Code Online (Sandbox Code Playgroud)

cObj = cObj + cObj2;
Run Code Online (Sandbox Code Playgroud)

释放内存泄漏程序.不知何故,问题是在重载+运算符后返回一个对象.

    #include <iostream>
    #include <vld.h>

    using namespace std;

    class Animal
    {
    public :
        Animal() {};
        virtual void eat()  = 0 {};
        virtual void walk() = 0 {};
    };

    class Dog : public Animal
    {
    public :
        Dog(const char * name, const char * gender, int age);
        Dog() : name(NULL), gender(NULL), age(0) {};

        virtual ~Dog();
        Dog operator+(const Dog &dObj);

    private :
        char * name;
        char * gender;
        int age; …
Run Code Online (Sandbox Code Playgroud)

c++

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

通过argv [1] C++打开文件

我检查了参数argv[1]是否对我的文件有效.

我在自己的子程序中有一个菜单系统,名为main.

读取文件等是在与第一个分开的另一个子例程中完成的,也在main中调用.

如何将转移argv[1]到第一个子程序?甚至第二个?

string sArgInit = argv[1];

这样我就可以使用C-String打开文件.

但是我不能把字符串带到main之外的任何函数..

有没有办法在没有:全局变量的情况下执行此操作,将字符串作为参数传递给子例程.

c++

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

ifstream object.eof()不起作用

我想我可能需要在我的while条件下使用布尔值bValue = false:

char cArray[ 100 ] = "";
ifstream object;
cout << "Enter full path name: ";
cin.getline( cArray, 100 );
if ( !object ) return -1   // Path is not valid? This statement executes why?

ifstream object.open( cArray, 100 );

// read the contents of a text file until we hit eof.
while ( !object.eof() )
{
// parse the file here

}
Run Code Online (Sandbox Code Playgroud)

为什么我不能输入文本文件的完整路径名?

这可能是因为eof.他们的语法是否可以模拟eof的布尔语句?

我能有......吗:

while ( !object == true )
{
// parase contents of file …
Run Code Online (Sandbox Code Playgroud)

c++

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

移动圆形阵列

我有一个家庭作业,要求必须在内存中移动一个圆形阵列-n个位置.

我已使用以下C++语法完成了该任务:

while ( r < 0 )  // rotate negatively.
{
    if ( i == top+1 )
    {
        current->n = items[top+1].n;  
        items[top+1].n = items[back-1].n;
    }
    midPtr->n  = items[++i].n;
    items[i].n = current->n;

    if ( i == back-1 )
    {
            items[back-1].n = current->n;
            i = 0;
            r++; continue;
    }
    else
    {
            current->n = items[++i].n;
            items[i].n = midPtr->n;
            if ( i == back-1 )
            {
                    i = 0;
                    r++; continue;
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有人有更好,更有效的方法将圆形阵列移动-n单位.

因为我似乎在ptr变量之间执行不必要的转移.

c++

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

标签 统计

c++ ×10

algorithm ×1

arrays ×1

file ×1

file-io ×1

formatting ×1

function ×1

rotation ×1

visual-studio ×1