小编use*_*120的帖子

如何将argv [1]传递给一个带0个参数的函数?

我究竟需要做什么才能将argv [1]的内容转换为不使用参数的函数?

这是如何运作的?

const char *cPtr = argv[1];
Run Code Online (Sandbox Code Playgroud)

并将其传递给带有0个参数的someFunction()!

c++ command-line-arguments

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

使用模板

我一直在尝试获取一个模板,将字符串中的字符转换为大写字母.

我需要在整个程序中多次这样做.

所以我会使用一个模板.

template <string theString>
string strUpper( string theString )
{
    int myLength = theString.length();
    for( int sIndex=0; sIndex < myLength; sIndex++ )
    {
        if ( 97 <= theString[sIndex] && theString[sIndex] <= 122 )
        {
        theString[sIndex] -= 32;
        }
    }   
   return theString;
}
Run Code Online (Sandbox Code Playgroud)

现在只有模板有效!有什么建议?'string'标识符应该是立即标志.

c++ templates

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

复制构造函数

我试图用我的拷贝构造函数复制"深拷贝"的一部分:

class myClass
{
public:

    myClass ( const char *cPtr, const float fValue )
    myClass ( const myClass& myClassT );

private:

    const char* &myAddress;
    float MyFloater;

};

//myClass.cpp

myClass::myClass( const char *cPtr, const float fValue )
{
// Initialize both private varaible types
   const char* &myAddress = cPtr;
   float myFloater = fValue;
}

myClass::myClass( const myClass& classType )
{
// copy what we did ... 
      myAddress = myClass.myAddress;
      myFloater = myClass.myFloater;
}
Run Code Online (Sandbox Code Playgroud)

只有这一点,我只得到,"必须初始化whataver基础/成员initalizer列表中的变量.

他们在构造函数中被初始化!我需要对classtype对象地址做什么?

c++

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

重载=在C++中

我正在尝试重载赋值运算符,并希望清除一些事情,如果可以的话.

我有一个非成员函数,bool operator==( const MyClass& obj1, const myClass& obj2 )定义了我班级的oustide.

出于显而易见的原因,我无法找到任何私人会员.

所以我认为我需要做的是重载赋值运算符.并在非成员函数中进行赋值.

话虽如此,我想我需要做以下事情:

  1. 使用我的功能并使用strcpy或复制信息strdup.我用过strcpy.
  2. 转到赋值运算符,bool MyClass :: operator =(const MyClass&obj1);
  3. 现在我们转到函数重载(==)并将obj2赋给obj1.

我没有复制构造函数,所以我坚持这些:

class Class
{
private:
m_1;
m_2;
public:
..
};

void Class::Func1(char buff[]) const
{   
    strcpy( buff, m_1 );
    return;
}
void Class::Func2(char buff[]) const
{
    strcpy( buff, m_2 );
    return;
}

bool Class& Class::operator=(const Class& obj)
{ 
    if ( this != &obj ) // check for self assignment.
    {
        strcpy( …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

0
推荐指数
2
解决办法
481
查看次数

指针问题2 [有效的C++语法]

这个版本正在运行.我在//整个代码中都发表了评论,以便更好地说明我遇到的问题.该程序依赖于读取文本文件.包含标点符号的段落格式.

可以将此和上述内容复制到文本文件中并运行该程序.

// Word.cpp

#define _CRT_SECURE_NO_WARNINGS // disable warnings for strcpy
#define ARRY_SZ 100
#include <iostream>
#include <fstream>
#include "Word.h"

using namespace std;

Word::Word( const char* word )
{
    ptr_ = new char[ strlen( word ) + 1 ];
    strcpy( ptr_, word  );  
    len_ = strlen( ptr_ );
}

Word::Word( const Word* theObject ) 
{
    ptr_ = theObject->ptr_;
    len_ = theObject->len_;
}

Word::~Word()
{
    delete [] ptr_;
    ptr_ = NULL;
}

char Word::GetFirstLetterLower()
{
    // I want to …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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