C ++编译错误

时间:2018-07-19 11:08:31

标签: c++ raspberry-pi

这是错误和程序。我给了这个错误,我在main.cpp中找不到任何拼写错误

错误

main.cpp: In member function ‘int GPIO::unexport_gpio()’:
main.cpp:27:45: error: conversion from ‘const char*’ to non-scalar type ‘std::ofstream {aka std::basic_ofstream<char>}’ requested
  ofstream unexportgpio = (unexport_str.c_str());
                          ~~~~~~~~~~~~~~~~~~~^~~
main.cpp:28:7: error: ‘exportgpio’ was not declared in this scope
  if(!(exportgpio.is_open())
       ^~~~~~~~~~
main.cpp:29:3: error: expected ‘)’ before ‘return’
   return -1;
   ^~~~~~
main.cpp:30:2: error: ‘exportgpio’ was not declared in this scope
  exportgpio<<this->gpionum;
  ^~~~~~~~~~
main.cpp: In member function ‘int GPIO::setdir_gpio(std::__cxx11::string)’:
main.cpp:36:64: warning: statement has no effect [-Wunused-value]
  std::string setdir_str = "/sys/class/gpio/gpio"+this->gpionum;+"/direction";
                                                                ^~~~~~~~~~~~~
main.cpp:37:41: error: conversion from ‘const char*’ to non-scalar type ‘std::ofstream {aka std::basic_ofstream<char>}’ requested
  ofstream setdirgpio = (setdir_str.c_str());
                        ~~~~~~~~~~~~~~~~~^~~
main.cpp: At global scope:
main.cpp:48:2: error: ‘setdirgpio’ does not name a type
  setdirgpio.close();
  ^~~~~~~~~~
main.cpp:49:2: error: expected unqualified-id before ‘return’
  return 0;
  ^~~~~~
main.cpp:50:1: error: expected declaration before ‘}’ token
 }
 ^
main.cpp: In member function ‘int GPIO::setdir_gpio(std::__cxx11::string)’:
main.cpp:47:2: warning: control reaches end of non-void function [-Wreturn-type]
  }

^

main.cpp

#include "main.h"

GPIO::GPIO()
{
        this->gpionum = "4";
}

GPIO::GPIO(std::string gnum)
{
        this->gpionum = gnum;
}

int GPIO::export_gpio()
{
        string export_str = "/sys/class/gpio/export";
        ofstream exportgpio(export_str.c_str());
        if(!(exportgpio.is_open()))
                return -1;
        exportgpio << this->gpionum;
        exportgpio.close();
        return 0;
}

int GPIO::unexport_gpio()
{
        std::string unexport_str = "/sys/class/gpio/unexport";
        ofstream unexportgpio = (unexport_str.c_str());
        if(!(exportgpio.is_open())
                return -1;
        exportgpio<<this->gpionum;
        return 0;
}

int GPIO::setdir_gpio(std::string x)
{
        std::string setdir_str = "/sys/class/gpio/gpio"+this->gpionum;+"/direction";
        ofstream setdirgpio = (setdir_str.c_str());
        if(!(setdirgpio.is_open()))
                return -1;

        if(x == "0")
        {
                setdirgpio<<"in";
        }
        if(x == "1")
                setdirgpio<<"out";
        }
        setdirgpio.close();
        return 0;
}

int GPIO::setval_gpio(std::string x)
{
        string setval_str = "/sys/class/gpio/gpio"+this->gpionum+"/value";
        ofstream setvalgpio = (setval_str.c_str());
        if(!(setvalgpio.is_open()))
                return -1;

        if(x == "0")
        {
                setvalgpio<<"0";
                setvalgpio.close();
        }
        if(x == "1")
                setvalgpio<<"1";
                setvalgpio.close();
        }
        return 0;
}

我正在尝试制作一个程序来切换RPI3上的状态。 我在Arduino上已经习惯了,但在RPI3上却不习惯。 如果您知道如何解决,请不要犹豫。 应该可以驱动我的花园喷头

1 个答案:

答案 0 :(得分:0)

ofstream unexportgpio = (unexport_str.c_str());

应该是

ofstream unexportgpio(unexport_str.c_str());

碰巧这是一段奇怪的代码

std::string unexport_str = "/sys/class/gpio/unexport";
ofstream unexportgpio(unexport_str.c_str());

为什么不这样做更简单?

ofstream unexportgpio("/sys/class/gpio/unexport");

无需从C字符串文字中生成std::string,仅需重新获得C字符串即可。