小编Ben*_*igt的帖子

在C++中添加序列中的素数

考虑一系列n个正整数,其中nn个数字在stdin上输入,计算素数的总和.

我怎么做?我尝试了以下代码但失败了:

#include <iostream>
using namespace std;
int main()
{
    int n, i, j, numero = 0, primo = 0, soma = 0;
    cout << "Digite um numero: ";
    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> numero;
        for (j = 1; j < n; j++)
        cout << " numero " << numero << endl;
        {
            if (numero %j == 0)
                primo ++;
                cout << "primo" …
Run Code Online (Sandbox Code Playgroud)

c++

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

使用带有C库函数的C++字符串

当我尝试在以下行中使用atoi(con​​st char*)函数时发生错误...

externalEncryptionRawHolder[u] = atoi(parser.next()); 
Run Code Online (Sandbox Code Playgroud)

'parser'对象是一个字符串解析器,'next'方法返回一个字符串.我认为错误与'atoi'函数中的字符串不是常数这一事实有关......但我不确定.错误的要点是'无法将字符串转换为const char*'.如何让我的弦不变?任何帮助都将非常感激(顺便说一句,如果你想知道索引'你'是什么,这是在'for'循环内).

c++ string

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

Dalvik对本机c ++代码性能的影响?

我计划使用Necessitas将Qt代码移植到Android平台.在第一眼看到我注意到尽管是本机代码,但一切仍然通过Dalvik VM.

我的问题是这会引入开销吗?Java开头的效率低于原生c ++,与vanilla Java相比,Dalvik相当不成熟,这是我担心的原因.

c++ performance qt dalvik

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

strrchr导致'无法从const char*转换为char*'

我正在尝试编译一些给我的代码,我被告知编译好了.也许在不同的编译器上.我正在使用VS2010,我有以下几行:

char *dot = strrchr(filename, '.');
Run Code Online (Sandbox Code Playgroud)

这会导致编译错误:

"错误C2440:'初始化':无法从'const char*'转换为'char*'

怎么会?我该如何解决?

c++ string visual-studio-2010

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

C++ 11移动语义和Microsoft Visual C++编译优化

我们考虑在Microsoft Visual C++(Microsoft Visual Studio 2012 RC,版本11.0.50522.1 RCREL)中考虑自定义数组的类模板.

/*C++11 switch-on*/

#include <iostream>

template <typename element, unsigned int size>
class array
{
    private:
        element data[size];
    public:
        array(){}
        ~array(){}
        array(const array & other)(){}
        element & operator [](unsigned int i)
        {
            if(i<size)
                return data[i];
            else
                throw std::runtime_error("Out of boundary");
        }
}
Run Code Online (Sandbox Code Playgroud)

请注意,构造函数,析构函数和复制构造函数被定义为不执行任何操作.简单的打印功能定义如下

/*printing*/

template <typename element, unsigned int size>
void print(test::array<element, size> & content)
{
    unsigned int i=0;
    for(std::cout<<"["<<content[i++];i<size;std::cout<<content[i++])
        std::cout<<",";
    std::cout<<"]"<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

当程序运行以下主要时

int main(int argc, char * argv[])
{
    array<int, 3> a; …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++ move-semantics c++11 visual-studio-2012

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

如何将函数的这个地址从C++写入C#?

我正在学习DirectX,但我不太了解C++所以我决定使用SharpDX.但我在C++中坚持使用TypedEventHandler中的函数地址,但是我不知道如何在C#中编写它?有人可以帮忙吗?

ref class App sealed : public IFrameworkView
{
public:
virtual void Initialize(CoreApplicationView^ AppView)
{
    AppView->Activated += ref new TypedEventHandler
        <CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
}
virtual void SetWindow(CoreWindow^ Window) {}
virtual void Load(String^ EntryPoint) {}
virtual void Run() {}
virtual void Uninitialize() {}

void OnActivated(CoreApplicationView^ CoreAppView, IActivatedEventArgs^ Args)
{
    CoreWindow^ Window = CoreWindow::GetForCurrentThread();
    Window->Activate();
}
};
Run Code Online (Sandbox Code Playgroud)

到目前为止我的转换

internal class App : IFrameworkView
{
    public void Initialize(CoreApplicationView AppView)
    {
        // Call OnActivated() when the Activated event is triggered
        AppView.Activated += new TypedEventHandler<CoreApplicationView, …
Run Code Online (Sandbox Code Playgroud)

c# directx windows-runtime c++-cx

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

使用整数指针从C#调用外部DLL

我正在尝试从C#调用外部.dll函数。dll的文档定义了以下功能:

int funcName(int *retVal)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了各种配置,并且总是尝试从p / invoke获得不平衡的堆栈错误。我的C#代码当前如下所示:

[DLLImport("dllName");
unsafe static extern int funcName(ref IntPtr retVal);
unsafe IntPtr retNum;
int status = funcName(ref retNum);
Run Code Online (Sandbox Code Playgroud)

任何想法表示赞赏!

c# c++ dll interop

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

如何使用私有继承的方法覆盖纯虚方法?

我有以下内容:

class Abstract
{
    virtual void AbstractMethod() = 0;
};

class Implementer
{
    void AbstractMethod() {};
};

class Concrete : public Abstract, private Implementer
{};
Run Code Online (Sandbox Code Playgroud)

我无法实例化,Concrete因为AbstractMethod不会覆盖纯虚方法.我究竟做错了什么?

c++ inheritance multiple-inheritance pure-virtual private-inheritance

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

C++ - 为什么在op + =方面实现op +而不是相反?

为什么这个实现:

T& T::operator+=( const T& ) {
  // ... implementation ...
  return *this;
}

T operator+( const T& lhs, const T& rhs ) {
  T temp( lhs );
  return temp += rhs;
}
Run Code Online (Sandbox Code Playgroud)

比这更频繁地传播:

T& T::operator+=( const T& rhs ) {
  *this = *this + rhs;
  return *this;
}

T operator+( const T& lhs, const T& rhs ) {
  // ... implementation ...
  return some_result;
}
Run Code Online (Sandbox Code Playgroud)

是否有任何理由,或者只是一个随机的巧合,我看到人们在我阅读的文献中多次这样实现它,而从来没有反过来?

c++ code-reuse operator-overloading

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

为什么当它应该像try/catch/finally一样使用时抛出异常?

据我所知,使用像try/catch/finally这样的工作,所以我希望如果在using语句中发生异常它会被捕获(这有点奇怪,因为这也意味着异常被默默地吃掉了).using语句应该捕获异常并调用Dispose方法,但是,没有发生.我已经设计了一个简单的测试来证明这个问题.

这是我强制在using语句中发生异常的地方:

using (TcpClient client = new TcpClient())
{
    // Why does this throw when the using statement is supposed to be a try/catch/finally?
    client.Connect(null);
}
Run Code Online (Sandbox Code Playgroud)

抛出异常client.Connect()(意味着它没有被using语句捕获或者被重新抛出):

System.ArgumentNullException: Value cannot be null.
Parameter name: remoteEP
   at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
   at DotNETSandbox.Program.Main(String[] args) in C:\path\to\Sandbox\Program.cs:line 42
Run Code Online (Sandbox Code Playgroud)

根据微软关于该主题的文章,如果Dispose方法抛出,则可能会抛出using语句.

但是,当我遵循使用模式时,显然Dispose方法不会抛出:

TcpClient c2 = new TcpClient();
try
{
    c2.Connect(null);               
}
catch (Exception e)
{
    // We caught the null ref exception
    try
    {
        // Try to dispose: …
Run Code Online (Sandbox Code Playgroud)

c# exception using-statement try-catch

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