小编Chr*_*ris的帖子

C++运算符重载

为什么下面的C++程序输出"ACCA"?为什么operator int()叫两次?

#include "stdafx.h"
#include <iostream>

using namespace std;

class Base {
public:
    Base(int m_var=1):i(m_var){
        cout<<"A";
    }
    Base(Base& Base){
        cout<<"B";
        i=Base.i;
    }
    operator int() {
        cout<<"C";
        return i;
    }
private:
    int i;
};

int main()
{
    Base obj;
    obj = obj+obj;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ class operator-overloading

15
推荐指数
4
解决办法
874
查看次数

MVC企业领域 - 好还是坏?

在单个项目解决方案中,当您拥有大量控制器时引入区域确实可以改善分离,并允许模块轻松地复制到解决方案中或从解决方案中复制出来.但是,在大型企业解决方案中,我倾向于将逻辑拆分为单独的项目.

因此具有单独的UI,Controller,SOA,Model和Repository项目.在这种情况下,区域不再有意义了,而且它们为Url添加了额外的顶层,这通常是不需要的,但我相信如果你保持控制器的独特性,你可以省略Url中的区域,但不是有点臭吗?

区域可能适用于中等复杂度的站点,或者模块代码更好地保存在一个位置,因此可以将其复制到其他站点或删除.

asp.net-mvc asp.net-mvc-areas asp.net-mvc-3-areas

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

如何使用Doctrine2和Symfony2查看自动生成的SQL架构更新SQL查询?

我在Symfony2项目中添加了一些数据库表.然后,我使用以下命令:

php app/console doctrine:generate:entities Acme
Run Code Online (Sandbox Code Playgroud)

更新类.然后,我尝试使用以下命令更新MySQL DB:

php app/console doctrine:schema:update --force
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误消息

[PDOException]                                                                              
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near
'like TINYINT(1) NOT NULL, created_at DATETIME NOT NULL, INDEX IDX_8EA4B675A76ED3' at line 1  
Run Code Online (Sandbox Code Playgroud)

如何查看哪个SQL查询错误?自动生成SQL查询的位置是什么?

提前致谢.

mysql sql symfony doctrine-orm

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

无法链接Boost正则表达式

我目前正在尝试通过KIT编译Contraction Hierachies实现,这需要Boost :: Regex.提供的Makefile已经确保(我也手动仔细检查过)g ++随-lboost_regex交换机提供.如果没有安装库,g ++会抱怨.

所以我从我的包源安装了库,并再次尝试编译.这次我得到了很多关于Boost :: Regex的链接器错误.这是一个简短的摘录:

main.o: In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb]+0x2c): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.o: In function `void boost::re_detail::raise_error<boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > >(boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::error_type)':
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0x7d): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xb1): undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xcb): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.o: In function `__gnu_cxx::__normal_iterator<char const*, std::string> boost::re_detail::re_is_set_member<__gnu_cxx::__normal_iterator<char const*, std::string>, char, boost::regex_traits<char, boost::cpp_regex_traits<char> >, unsigned int>(__gnu_cxx::__normal_iterator<char …
Run Code Online (Sandbox Code Playgroud)

linker gcc boost linker-errors boost-regex

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

尽管使用unique_ptr,内存泄漏

我有一个类的构造函数,它初始化该类内部的unique_ptr,并传递一个值.出于某种原因,valgrind抱怨内存泄漏:

22,080 (24 direct, 22,056 indirect) bytes in 1 blocks are definitely lost in loss record 6 of 6
   at 0x4C2C7A7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x4A64FB: VectorBasedNodeOrder::VectorBasedNodeOrder(VectorBasedNodeOrder const&) (VectorBasedNodeOrder.cpp:33)
   /* snip more trace */

这是所谓的违规代码,剥夺了所有不相关的东西:

class VectorBasedNodeOrder : public NodeOrder
{
public:
    VectorBasedNodeOrder(const VectorBasedNodeOrder& order);
protected:
    std::unique_ptr<std::vector<Node*>> orderedNodes;
}

VectorBasedNodeOrder::VectorBasedNodeOrder(const VectorBasedNodeOrder& order):
NodeOrder(order),
orderedNodes(unique_ptr<std::vector<Node*>>(
    new std::vector<Node*>(*(order.orderedNodes)))) // <-- line 33
{
}
Run Code Online (Sandbox Code Playgroud)

你能解释为什么以及如何发生内存泄漏?

c++ memory-leaks unique-ptr

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

ASP.NET Web API操作方法参数的依赖注入

我正在使用C#中的ASP.NET Web API项目来获取移动应用程序的JSON接口.我的想法是为所有请求创建接口,然后仅在Web API代码中使用这些接口.

我最终得到了这样的东西:

public interface IApiObject {}
public interface IApiResponse<T> : IApiObject where T : IApiObject {}
public interface IApiRegistrationRequest : IApiObject {}
Run Code Online (Sandbox Code Playgroud)

我的控制器看起来像这样:

public class MyApiController : ApiController
{

    public IApiResponse<IApiObject> Register(IApiRegistrationRequest request) {
        // do some stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Web API项目还包含这些接口的实现.

我假设Web API项目使用像MVC项目那样的模型绑定,所以我创建了一个继承感知的ModelBinderProvider,用于为所有IApiObjects提供一个绑定器,并使用Unity容器来解析其实现的接口.

但是,经过一番调查,我发现了Web API 如何进行参数绑定,并发现Web API使用格式化程序而不是复杂类型的模型绑定程序.链接的博客文章建议在我的操作参数上使用ModelBinderAttribute,但该属性仅接受类型作为参数.但是,我的自定义模型绑定器不包含空构造函数(它需要一个统一容器),所以我需要传递它的一个实例.

我能想到的另一种方法是使用依赖注入格式化程序.不幸的是,我不熟悉它们,因为我以前从未使用它们.

哪种方式正确?我该怎么办?

c# dependency-injection asp.net-web-api

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

从HTML帮助程序中提取显示名称和描述属性

我正在构建一个如下所示的自定义HTML.LabelFor助手:

public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Boolean showToolTip)
{
  var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
  ...
}
Run Code Online (Sandbox Code Playgroud)

为了能够获得属性的正确名称,我使用以下代码:

metadata.DisplayName
Run Code Online (Sandbox Code Playgroud)

在ModelView类的属性上我得到了:

[DisplayName("Titel")]
Run Code Online (Sandbox Code Playgroud)

问题是我还需要一个描述.有一个名为Display的属性,它有名称和描述,但我没有看到如何使用上面代码中的元数据变量来提取它?

c# reflection attributes html-helper asp.net-mvc-2

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

为什么Symfony在prod环境中缺少dev bundle?

我的Symfony应用程序有一些依赖项,只有开发,测试等才需要.这些在我composer.jsonrequire-dev部分中定义.

以下是我添加它们的方法AppKernel.php:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            // ...
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
            $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
            $bundles[] = new Liip\FunctionalTestBundle\LiipFunctionalTestBundle();
        }

        return $bundles;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我更新我的应用程序时,我运行php composer.phar install --no-dev --optimize-autoloader.这将安装开发环境不需要的所有要求,然后清除缓存.

但是,清除缓存失败,并显示以下消息:

PHP Fatal error:  Class 'Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle' not found in /my/project/app/AppKernel.php on line 29
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling …

php symfony composer-php

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

从优先级队列中获取unique_ptr

我正在维护一组unique_ptr实例priority_queue.在某些时候,我想获得第一个元素并将其从队列中删除.但是,这总是会产生编译器错误.请参阅下面的示例代码

int main ()
{
  std::priority_queue<std::unique_ptr<int>> queue;
  queue.push(std::unique_ptr<int>(new int(42)));

  std::unique_ptr<int> myInt = std::move(queue.top());
  return 1;
}
Run Code Online (Sandbox Code Playgroud)

这会产生以下编译器错误(gcc 4.8.0):

uptrtest.cpp: In function ‘int main()’: uptrtest.cpp:6:53: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]’    std::unique_ptr<int> myInt = std::move(queue.top());
                                                     ^ In file included from /usr/include/c++/4.8/memory:81:0,
                 from uptrtest.cpp:1: /usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^
Run Code Online (Sandbox Code Playgroud)

更改要queue此问题中使用的代码可以解决问题并且代码编译得很好.

有没有办法保持unique_ptrs priority_queue或我错过了什么?

c++ priority-queue unique-ptr c++11 gcc4.8

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

参数属性只允许在构造函数实现中

所以我写了这段代码,这一行use(private lang: string): Promise<object>给了我一个错误“A parameter property in only allowed in constructor implementation”

当我删除访问修饰符时它会起作用,private但我只是好奇为什么它会给我这个错误以及正确的方法是什么?

@Injectable()
export class TranslateService {

  public data: object = {};

  constructor(private http: HttpClient) {}

  use(private lang: string): Promise<object> {
    return new Promise<object>((resolve, reject) => {
      const langPath = `assets/i18n/${lang || 'en'}.json`;

      this.http.get<object>(langPath).subscribe(
        translation => {
          this.data = Object.assign({}, translation || {});
          resolve(this.data);
        },
        error => {
          this.data = {};
          resolve(this.data);
        }
      );
    });
  }

}
Run Code Online (Sandbox Code Playgroud)

typescript angular

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