小编Man*_*rse的帖子

为什么这个C++程序使用构造函数和析构函数显示错误?

以下程序显示错误:

#include<conio.h>
#include<iostream.h>
int count = 0;
class alpha
   {
   public:

   alpha()
         {
         count++;
         cout<<"\n Number of objects created "<<count; 
         }
  ~alpha()
         {
         cout<<"\n Number of object destroyed"<<count;
         count--;
         }
  };

int main
{
    cout<<" inside main ";
    alpha a1, a2, a3, a4;
    {
        cout<<"\n Block 1 ";
        alpha A5;
    }
    {
        cout<<"\n Block 2 ";
        alpha a6;
    }
    cout<<" main again ";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

第11行:错误:由于-Wfatal-errors,对'count'的引用是模糊的编译终止.

c++ constructor destructor

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

浮点数不能按预期工作

在下面的代码中,当我输入为1 10 2 1 2 2时,总和打印为52,sum3打印为31.200001,而它的输入为31.200000

int main(){

    int t,n,i,a[2000],m,j,f;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        scanf("%d",&f);
        for(i=0;i<f;i++){
            scanf("%d",&a[i]);
        }
        scanf("%d",&m);
        if(n!=0){
            int sum=n*(n+1)/2;
            int sum2=0;
            for(j=0;j<i;j++){
                sum2+=a[j];
            }
            sum-=sum2;
            printf("%d\n",sum);
            float sum3;
            if(n%2==0) sum3=(1.0-2.0*m/n)*sum;
            else sum3=(1.0-2.0*m/(n+1))*sum;
            printf("%f\n",sum3);
        }
        else printf("0.0000\n");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c floating-point

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

无法使用多态派生创建基类的shared_pointer

我有一个基本多态类(使用虚方法)并从中派生出来.我想使用以下代码

     boost::shared_ptr<base_class> ptr( new derived_class() );
Run Code Online (Sandbox Code Playgroud)

但编译器返回以下错误

    cannot convert ‘fpga_northwest*’ to ‘fpga*’ in initialization
    make: *** [../obj/ixecute_cmd_interface.o] Error 1
Run Code Online (Sandbox Code Playgroud)

看看周围我很想使用以下构建正常,但我有一些疑问.你认为这是正确的吗?

     boost::shared_ptr<base_class> ptr_base;
     boost::shared_ptr<derived_class> ptr_derived( new derived_class() );
     ptr_base = boost::dynamic_pointer_cast<base_class>( ptr_derived );
Run Code Online (Sandbox Code Playgroud)

如果我使用boost::static_pointer_cast我有编译器错误; 既然我从一个衍生到一个基础的铸造应该不是更正确的一个static_cast?

谢谢你的帮助

c++ casting shared-ptr

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

for循环的操作

我在嵌入式c工作了一个月,直到现在我遇到了简单的循环,如:

for(i=0;i<10;i++)
Run Code Online (Sandbox Code Playgroud)

但现在我遇到了一种新的for循环,它是:

for(t=0; string[t]; ++t)
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我这个循环是如何工作的.示例代码是:

#include <stdio.h>
#include <ctype.h>

void print_upper(char *string);

int main(void)
{
    char s[80];
    printf("Enter a string: ");
    gets(s);
    print_upper(s);
    printf(''\ns is now uppercase: %s", s);
    return 0;
}

/* Print a string in uppercase. */
void print_upper(char *string)
{
    register int t;
    for(t=0; string[t]; ++t)
    {
        string[t] = toupper(string[t]);
        putchar(string[t]);
    }
}
Run Code Online (Sandbox Code Playgroud)

c

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

如何使用std :: for_each?

是否可以使用std :: for_each或其他类似的东西?

#include <list>
#include <algorithm>

// Class declaration
//
struct Interface
{
   virtual void run() = 0;
};

struct A : public Interface
{
   void run() { std::cout << "I run class A" << std::endl; }
};

struct B : public Interface
{
   void run() { std::cout << "I run class B" << std::endl; }
};

// Main
//
int main()
{
   // Create A and B
   A a;
   B b;

   // Insert it inside a list
   std::list<Interface …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm foreach

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

表达式如何评估?

有人可以为表达式绘制优先级树,请在C中进行表达式求值后解释副作用.值.

int i=-3, j=2, k=0, m;
m= ++i || ++j&&++k;
Run Code Online (Sandbox Code Playgroud)

根据我输出应该是-2 3 1 1但我的gnu c编译器打印是-2 2 0 1?我想知道怎么样?

c expression

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

如何/应该隐藏C++静态成员变量和函数?

m_MAX和ask()由run()使用,但不应该是公共的.怎么能/应该这样做?

#include <vector>
class Q {
public:
    static int const m_MAX=17;
    int ask(){return 4;}
};
class UI {
private:
    std::vector<Q*> m_list;
public:
    void add(Q* a_q){m_list.push_back(a_q);}
    int run(){return Q::m_MAX==m_list[0]->ask();}
};
int main()
{
    UI ui;
    ui.add(new Q);
    ui.add(new Q);
    int status = ui.run();
}
Run Code Online (Sandbox Code Playgroud)

c++ encapsulation c++11

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

继承和构造函数的问题

在定义我的Element类时,我收到以下错误:

no matching function for call to ‘Dict::Dict()’ word1.cpp:23:
    note: candidates are: Dict::Dict(std::string) word1.cpp:10:
    note: Dict::Dict(const Dict&)
Run Code Online (Sandbox Code Playgroud)

我没有打电话给任何人,我只是让Element从Dict继承.这有什么问题?

另一个错误是在我的Word类构造函数中我得到以下内容:

In constructor ‘Word::Word(std::string)’: word1.cpp:71:
    note: synthesized method ‘Element::Element()’ first required here
Run Code Online (Sandbox Code Playgroud)

在这一点上我真的很沮丧,因为对我来说一切似乎都没问题.

#include <iostream>
#include <vector>
#include <sstream>
#include <string.h>
#include <fstream>

using namespace std;

class Dict {
   public:
    string line;
    int wordcount;
    string word;
    vector <string> words;

    Dict(string f) {
        ifstream in(f.c_str());
        if (in) {
            while (in >> word) {
                words.push_back(word);
            }
        }
        else cout << "ERROR couldn't open file" …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

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

GLSL中的反射映射

我正在尝试使用在OpenGL ES 2.0中工作的立方体贴图进行反射贴图.我不确定我是否正确计算反射方向以传递到片段着色器中的samplerCube.

首先,我有Projection,Model和View矩阵以及一个组合的ModelView矩阵.这是我在GLSL中的顶点着色器的代码:

attribute vec4 Position;
attribute vec3 Normal;

uniform mat4 Projection;
uniform mat4 ModelView;
uniform mat3 Model;
uniform mat4 ModelViewProjectionMatrix;
uniform vec3 EyePosition;

varying vec3 ReflectDir;


void main()
{
    gl_Position = Projection * ModelView * Position;
    mediump vec3 EyeDir = normalize(Position.xyz - EyePosition);

    // Reflect eye direction by normal, and transform to world space
    ReflectDir = Model * reflect(EyeDir, Normal);
}
Run Code Online (Sandbox Code Playgroud)

我的假设是EyeDirection必须在对象空间中.(正确?)如何在对象空间中获取EyePosition?我是否只是通过逆ModelView矩阵变换摄像机位置(即总是在OpenGL中的{0,0,0}?)?

我试过这个但是我的片段着色器将所有颜色都着色,好像ReflectDir不正确.

有没有人有一个在Xcode中工作的OpenGL ES 2.0反射映射(带有samplerCube)的工作示例?

reflection opengl-es glsl ios

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

"蛇"运行错误

我正在用c ++制作一个简单的蛇游戏.它编译,但不会运行.我在mac上使用netbeans,我以前从来没有这样的问题,所以我认为这是我的代码的问题:

#include <vector>
std::vector<std::vector<int> > snake;//the snake
int main(){
    snake[0][0]=0;
}
Run Code Online (Sandbox Code Playgroud)

我认为它可能是2d矢量的问题.

c++ runtime-error 2d-vector

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

C++中goto语句的替代方法

以下是该流程图的链接:http: //i1146.photobucket.com/albums/o530/HTHVampire/C%20plus%20plus/Capture_zps5a6c3acf.jpg

在此输入图像描述

这是所示流程图的代码,只是忽略流程图中的模糊语句.

#include <iostream>
using namespace std;

int main()
{
    //declare and initialize a variable
    int a = 0;
    //prompt user for a value
    cout << "please enter a value" << endl;
    cin >> a;

    again1:
    //enter a decision block
    if(a > 10)
    {
        if(a < 10)
        {
            again2:
            if(a < 100)
            {
                a = a - 3;
                goto again2;
            }
            else goto again1;
        }
        else
        {
            a = a - 7;
            goto again1;
        }
    }
    else …
Run Code Online (Sandbox Code Playgroud)

c++ if-statement goto

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