草庐IT

c++ - "' void* ' is not a pointer-to-object type"在没有 void* 的代码中?

coder 2024-02-06 原文

我的代码有问题。在 Xcode 或使用 C++11 编译器中,此代码运行良好。但是,当我将此代码提交给在线法官时,判决显示“编译错误”。我认为他们使用的是 C++4.7.1 编译器,当我尝试编译它(使用 Ideone)时,它说:

prog.cpp: In function 'void printArray(int)':
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type
prog.cpp:27: error: 'void*' is not a pointer-to-object type

这是没有意义的,因为在这个程序的任何地方都没有void*

不过我不知道要更改什么。如果你们帮我解决这个问题,那就太好了。下面是我的代码:

#include <iostream>
#include <math.h>
#include <cmath>
#include <tgmath.h>

int array[10] = {1,2,3,4,5,6,7,8,9};
int initial = 1;
int tmp;
int total[500];

using namespace std;

void swap(int x, int y){
    int temp = array[x];
    array[x]=array[y];
    array[y]=temp;

    return;
}

void printArray(int size){

    int i;
    for (i=0;i<size;i++){
        //cout<<array[i]<<" ";
        tmp= array[i];
        tmp= (tmp* (pow(10.0,(size-i-1))));  // <--- Error here
        total[initial]=total[initial]+ tmp;
    }
    initial++;
    tmp=0;
    //cout<<endl;
}

void permute(int k,int size){
    int i;
    if (k==0) printArray(size);
    else{
        for (i=k-1;i>=0;i--){
            swap(i,k-1);
            permute(k-1,size);
            swap(i,k-1);
        }
    }

    return;
}

void quickSort(int arr[], int left, int right) {
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];

    while (i <= j) {
        while (arr[i] < pivot)
            i++;
        while (arr[j] > pivot)
            j--;
        if (i <= j) {
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    };

    if (left < j)
        quickSort(arr, left, j);
    if (i < right)
        quickSort(arr, i, right);
}

int main(){
    int countertest;
    cin>>countertest;
    int ak, asize;

    for(int a= 0; a<countertest; a++){

        initial = 1;
        std::fill(total, total+500, 0);
        cin>>asize>>ak;
        permute(asize,asize);

        quickSort(total, 1, initial-1);

        int arraydex [10000], temp = total[ak];
        for(int z = asize; z>=0; z--){
            arraydex[z] = temp % 10;
            temp /= 10;
        }

        for(int bc = 1; bc<=asize; bc++){
            cout<<arraydex[bc]<<" ";
        }
        cout<<endl;
    }

    return 0;
}

最佳答案

这里的问题是 <tgmath.h> 之间的冲突和 <cmath>/<math.h> .

这是我打开源文件到 <tgmath.h> 时发现的内容在我的 Linux 系统上:

#define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)

这很重要 - 它正在重新定义 pow使用这个其他宏。这立即是个问题,因为它是#define -远离正常pow功能。

那么这个函数有什么作用呢?好吧,让我们看看__TGMATH_BINARY_REAL_IMAG :

# define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
 (__extension__ (((sizeof (__real__ (Val1)) > sizeof (double)       \
       || sizeof (__real__ (Val2)) > sizeof (double))       \
      && __builtin_classify_type (__real__ (Val1)       \
          + __real__ (Val2)) == 8)    \
     ? ((sizeof (__real__ (Val1)) == sizeof (Val1)        \
   && sizeof (__real__ (Val2)) == sizeof (Val2))        \
  ? (__typeof ((__tgmath_real_type (Val1)) 0        \
       + (__tgmath_real_type (Val2)) 0))        \
    __tgml(Fct) (Val1, Val2)            \
  : (__typeof ((__tgmath_real_type (Val1)) 0        \
       + (__tgmath_real_type (Val2)) 0))        \
    __tgml(Cfct) (Val1, Val2))            \
     : (sizeof (__real__ (Val1)) == sizeof (double)       \
  || sizeof (__real__ (Val2)) == sizeof (double)        \
  || __builtin_classify_type (__real__ (Val1)) != 8     \
  || __builtin_classify_type (__real__ (Val2)) != 8)    \
     ? ((sizeof (__real__ (Val1)) == sizeof (Val1)        \
   && sizeof (__real__ (Val2)) == sizeof (Val2))        \
  ? (__typeof ((__tgmath_real_type (Val1)) 0        \
       + (__tgmath_real_type (Val2)) 0))        \
    Fct (Val1, Val2)              \
  : (__typeof ((__tgmath_real_type (Val1)) 0        \
       + (__tgmath_real_type (Val2)) 0))        \
    Cfct (Val1, Val2))              \
     : ((sizeof (__real__ (Val1)) == sizeof (Val1)        \
   && sizeof (__real__ (Val2)) == sizeof (Val2))        \
  ? (__typeof ((__tgmath_real_type (Val1)) 0        \
       + (__tgmath_real_type (Val2)) 0))        \
    Fct##f (Val1, Val2)             \
  : (__typeof ((__tgmath_real_type (Val1)) 0        \
       + (__tgmath_real_type (Val2)) 0))        \
    Cfct##f (Val1, Val2))))

嗯...我只是不知道该说什么。这是残酷和不寻常的。

所以这里发生了什么?好吧,我看了the docs for tgmath.h 并发现此 header 为 pow 等基本功能定义了一堆类型感知宏等将确定参数类型是否为 double , float等,然后将调用分派(dispatch)给适当的函数。这是因为 C 不支持重载(尽管 C++ 支持),所以它必须由魔术头文件提供。这里的重要细节是该函数只能在类型为 double 的参数上调用。 , float ,或这些类型的复杂版本,并且您正在传递 int .由于这里发生了宏替换,编译器甚至没有查看 cmath。函数,而不是直接跳入 C Macro From Hell,它试图对类型进行内省(introspection)。

简而言之,不要在C++中包含这个头文件!在C++中完全没有必要,因为已经支持重载函数,并且有一个complex。那里有模板来处理复数。

希望这对您有所帮助!

关于c++ - "' void* ' is not a pointer-to-object type"在没有 void* 的代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705016/

有关c++ - "' void* ' is not a pointer-to-object type"在没有 void* 的代码中?的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  5. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  6. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  7. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  10. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

随机推荐