小编Mah*_*esh的帖子

非重复随机数发生器

我想创建一个不重复它已经发出的数字的数字生成器(C++).

我所知道的是:

int randomgenerator(){
  int random;
  srand(time(0));
  random = rand()%11;
  return(random);
} // Added this on edition
Run Code Online (Sandbox Code Playgroud)

那个功能给了我多余的数字.

我正在尝试创建一个问卷程序,以随机顺序发出10个问题,我不希望任何问题重新出现.

有谁知道语法?

c++ random generator

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

VS 2012 - XP问题

我也安装了VS 2012 Professional和XP更新.我v110_xp在VS 2012上使用平台工具集构建了我的项目.我的项目的.msi包在Win 7上正常安装但在Win XP SP3上失败.XP SP3上报告的错误是 -

"程序入口点FlushProcessWriteBuffers无法位于动态链接库Kernel32.dll中".

虽然从VS 2005构建的相同项目在XP SP3上安装正常.我不确定VS 2012会发生什么._WIN32_WINNT设置为0x0501.有人可以指导如何解决问题?

任何帮助都非常感谢,

马赫什.

c++ windows-xp visual-studio-2012

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

指向#define的指针

我只是想知道是否有可能指向#define常量的指针.如果是的话,该怎么办?

c c-preprocessor

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

数组分配

让我用一个例子来解释 -

#include <iostream>

void foo( int a[2], int b[2] ) // I understand that, compiler doesn't bother about the
                               // array index and converts them to int *a, int *b
{
    a = b ;  // At this point, how ever assignment operation is valid.

}

int main()
{
    int a[] = { 1,2 };
    int b[] = { 3,4 };

    foo( a, b );

    a = b; // Why is this invalid here.

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

是因为,当传递给函数时,数组衰减到指针,可以进行foo(..) …

c++ arrays

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

SDL链接器错误

以下代码非常简单.它只是使用SDL以背景将图像绘制到屏幕上.我之前能够运行的程序运行良好,引用一切没有问题.

然而,现在,编译器抱怨.

无论如何,这是代码:

#include <iostream>
#include <string>
#include "SDL/SDL.h"

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

using std::string;

SDL_Surface * load_image(std::string filename)
{
    SDL_Surface * loadedImage = NULL;

    SDL_Surface * optimizedImage = NULL;

    loadedImage = SDL_LoadBMP(filename.c_str());

    if (loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);

        SDL_FreeSurface(loadedImage);
    }

    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface * destination)
{
    SDL_Rect offset;

    //Give offsets to the rectangle
    offset.x = x;
    offset.y …
Run Code Online (Sandbox Code Playgroud)

c++ sdl runtime-error

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

typeid运算符的奇怪行为?

使用XCode 3.2.3(64位),我得到以下奇怪的输出.我究竟做错了什么?

#include <iostream>
#include <typeinfo>

struct student {

};

int main()  
{  
    int i;
    student obj;

    std::cout << typeid(i).name() << "\n";
    std::cout << typeid(obj).name() << "\n";

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

输出:

i  
7student
Run Code Online (Sandbox Code Playgroud)

c++ xcode typeid

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

为什么不允许在std :: auto_ptr上使用operator []

为什么std :: auto_ptr上不允许使用operator []?

#include <iostream>

using namespace std ;

template <typename T>
void foo( T capacity )
{
    auto_ptr<T> temp = new T[capacity];

    for( size_t i=0; i<capacity; ++i )
        temp[i] = i; // Error
}

int main()
{
    foo<int>(5);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在Microsoft Visual C++ 2010上编译.

错误:错误C2676:二进制'[':'std :: auto_ptr <_Ty>'未定义此运算符或转换为预定义运算符可接受的类型

c++ auto-ptr visual-c++-2010

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

错误3699,为对象创建GUID

我正在尝试使用CoCreateGuid(GUID*guid)为我的应用程序中的对象创建一个guid.尝试使用此功能时出现一些错误.

error C3699: '*' : cannot use this indirection on type 'IServiceProvider' in servprov.h
error C2872: 'IServiceProvider' : ambiguous symbol in servprov.h
error C2371: 'IServiceProvider' : redefinition; different basic types in servprov.h
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码,目前并不多.

#include "Stdafx.h"
#include "AutomationCPP.h"
#include <Rpc.h>
#include <Guiddef.h>

using namespace System;

void AutomationCPP::CustomAutomationCPP::Instantiate()
{
    long result;
    unsigned char *guidstr;
    HRESULT hr;
    GUID *UIAguid;

    CoCreateGuid(UIAguid);  //errors here
}
Run Code Online (Sandbox Code Playgroud)

它说要为参数使用GUID指针,但我不断收到这些错误.非常感谢帮助!

c++ c++-cli visual-c++

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

使用fscanf()将整数复制到数组中.在C.

我有一个包含10行整数的文件.

我想将这些整数复制到几个数组中,名为line1,line2,line3等,其中数字来自相应数组中的每一行.

我目前正在使用

fscanf(items, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d", &line1[0], &line1[1], &line1[2], &line1[3], &line1[4], &line1[5], &line1[6], &line1[7], &line1[8], &line1[9]);
Run Code Online (Sandbox Code Playgroud)

将前10个int分配给名为line1的数组.

这是一个非常草率的方法,并没有解决将其余值分配给其他数组的另一个问题.

关于如何更整齐地为数组分配10个值的任何建议,以及如何将每一行分配给不同的数组将不胜感激!

谢谢

c arrays scanf

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

Android NDK中的C++模板

我正在使用Android NDK r6和Android SDK API 8(2.2).

我正在尝试使用模板实现像std :: vector这样的动态列表,但是我在编译的.o文件中遇到了很多错误.

一个样品:

错误

正如您所看到的,错误是在已编译的.o文件中生成的,而不是在源文件中生成的.

班级定义:

template <class T>
class ArrayList{
    private:
        int mSize;

    public:
        /**
         * Construye una lista dinámica vacía.
         */
        ArrayList ();

        /**
         * Destructor.
         */
        ~ArrayList ();

        /**
         * Añade un elemento a la lista.
         *
         * @param element
         *              Elemento.
         */
        void add (T element);

        /**
         * Obtiene un elemento de la lista.
         *
         * @param index
         *              Índice del elemento. Rango válido de valores: [0, size()]
         * @return …
Run Code Online (Sandbox Code Playgroud)

c++ templates android-ndk

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