小编Moh*_*mel的帖子

数字类型和字符串的模板函数和

我正在玩模板.我有以下模板来总结矢量的元素:

#include<vector>
#include <iostream>
template <class summable>
summable sum (const std::vector<summable> data, summable initial_value = 0){
    std::cout<<"in the function"<<std::endl;
    for (auto i : data){
        initial_value += i;
    }
    return initial_value;
}
Run Code Online (Sandbox Code Playgroud)

它对数字类型工作得非常好.但是如果我尝试传递一个字符串向量,我不会得到任何编译错误但是函数没有被调用.这是我的功能main:

int main(int argc, char** argv) {

    vector<string> s {"Hello" , " ", "There" };


    cout<<"Before calling the function\n";
    cout<<sum(s);

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

我得到了

在调用函数之前

作为输出.如果我改变函数调用 cout<<sum(s, string(" "));函数按预期工作.我猜它与我定义默认参数的方式有关,因为0它不是字符串的有效值(我认为).
我的问题是为什么我没有收到任何错误?因为它能够编译它应该运行,不是吗?

c++ templates

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

Scala:理解 Future.recover

我正在玩Future.recover(通过 intelJ 中的 Scala 表,如果它有任何重要性)

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

def get():Future[Int] = {
  throw new ClassCastException
}

val n = get().map{
  x => x + 1

}.recover{
  case e: Throwable => print("we recovered")
    0
}

n.map(print(_)) // not getting here
Run Code Online (Sandbox Code Playgroud)

我期待0被打印。但是,这是我得到的:

java.lang.ClassCastException
    at #worksheet#.get(test.sc:5)
    at #worksheet#.n$lzycompute(test.sc:8)
    at #worksheet#.n(test.sc:8)
    at #worksheet#.get$$instance$$n(test.sc:8)
    at A$A76$.main(test.sc:32)
    at #worksheet#.#worksheet#(test.sc)
Run Code Online (Sandbox Code Playgroud)

为什么我的recover不工作。我使用它不正确吗?

scala future

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

指针和函数参数

下面的代码片段给出了输出11 11未定义的值.但是为什么我在第二次执行相同的语句时得到未定义的值?这与函数的范围有什么关系吗?

   void foo(int **const p)
        {
            int j = 11;
            *p = &j;
            printf("%d ", **p);
        }

    int main()
        {
            int i = 10;
            int *p = &i;
            foo(&p);
            printf("%d ", *p);
            printf("%d ", *p);
        return 0;
        }
Run Code Online (Sandbox Code Playgroud)

c

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

是否有办法避免在 Xamarin 中每个方法的 setter 中调用 NotifyChanged ?

这个问题说明了一切。使用 Xamarin 时会出现这种模式:

public string Property
        {
            get
            {
                return this.propertyField;
            }

            set
            {
                if (value != this.propertyField)
                {
                    this.propertyField = value;
                    NotifyPropertyChanged();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

看来我必须为我拥有的每处房产都这样做。有什么办法可以避免重蹈覆辙吗?

c# mvvm xamarin xamarin.forms

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

在公共类中声明私有变量时,在java中非法启动表达式

import java.util.InputMismatchException;

    import java.util.Scanner;

    import java.util.Stack;

public class TSPNearestNeighbour {
 {

        private final Stack<Integer> stack;
        private int numberOfNodes;
        public TSPNearestNeighbour()

        {

            stack = new Stack<Integer>();

        }



        public void tsp(int adjacencyMatrix[][])

        {

            numberOfNodes = adjacencyMatrix[1].length - 1;

            int[] visited = new int[numberOfNodes + 1];

            visited[1] = 1;

            stack.push(1);

            int element, dst = 0, i,cost=0;

            int min = Integer.MAX_VALUE;

            boolean minFlag = false;

            System.out.print(1 + "\t");



            while (!stack.isEmpty())

            {

                element = stack.peek();

                i = 1;

                min = Integer.MAX_VALUE;

                while (i <= numberOfNodes) …
Run Code Online (Sandbox Code Playgroud)

java

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

为Makefile添加C++ 11支持

我试图在Ubuntu上使用make命令构建一个库,我收到此错误消息:

In file included from /usr/include/c++/4.7/cstdint:35:0,
                 from /home/mohammad/face-analysis-sdk-stable/src/utils/helpers.hpp:26,
                 from /home/mohammad/face-analysis-sdk-stable/src/utils/command-line-arguments.cpp:21:
/usr/include/c++/4.7/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
make[2]: *** [src/utils/CMakeFiles/utilities.dir/command-line-arguments.cpp.o] Error 1
make[1]: *** [src/utils/CMakeFiles/utilities.dir/all] Error 2
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)

我知道我需要在某处添加C++ 11标志,但我之前从未使用过make文件,我查看了make文件,我找不到应该在哪里添加它.我找不到任何关于g ++,或编译标志或任何东西的提及.

以下是make文件的一部分,请你指点小麦我应该编辑吗?

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8

# Default target …
Run Code Online (Sandbox Code Playgroud)

c++ makefile c++11

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

标签 统计

c++ ×2

c ×1

c# ×1

c++11 ×1

future ×1

java ×1

makefile ×1

mvvm ×1

scala ×1

templates ×1

xamarin ×1

xamarin.forms ×1