简体   繁体   中英

“+”: cannot add two pointers

OS: Windows 8.1 64bit | IDE: Visual Studio 2018

https://pastebin.com/6Lh6kABe -- if you need proper formatted code.

I'm developing a small command line tool to take screenshots using ADB. (Detail, ADB will be included in the same directory as the app; currently not there though.)

After fixing over 30 errors in my code that stopped the build process, now I'm here. An hour went by and I am not able to fix it, so I decided to ask you on here.

Here is the code:

    // ADBSS.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
//

#include <pch.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <tchar.h>

using namespace std;

int main(int argc, char** argv) {
    std::string filename;
    filename = "a";
    SetConsoleTitle(_T("*-_ ADB Screenshooter _-*"));
    std::cout << "+---------------------------------+" << endl;
    std::cout << "|ADB Screenshooter   [v1.0]       |" << endl;
    std::cout << "|Take screenshots from your device|" << endl;
    std::cout << "|with a simple CLI tool.          |" << endl;
    std::cout << "+---------------------------------+" << endl;
    cout << "Welcome to ADB Screenshooter." << endl;
    cout << "Please input the filename: (The screenshot will be saved with that name)" << endl;
    cout << "DO NOT INCLUDE ANY SPACES IN THE FILENAME. Use only letters." << endl;
    cin >> filename;
    Sleep(4);
    system("cls");
    SetConsoleTitle(_T("*-_ Taking the screenshot _-*"));
    cout << "Trust your computer now if you haven't before." << endl;
    system("adb shell screencap -p /sdcard/ADBScreenshooter/" + filename.c_str() + ".png");
    Sleep(4);
    system("cls");
    SetConsoleTitle(_T("*-_ Copying to PC! _-*"));
    cout << "The file will now be copied to the location from where you run ADB Screenshooter." << endl;
    system("adb pull /sdcard/" + filename.c_str() + ".png");
    Sleep(4);
    system("cls");
    SetConsoleTitle(_T("*-_ Done! _-*"));
    cout << "Everything is done! Thanks for using ADBSS. Press any key to finish." << endl;
    system("pause>nul");
    return 0;
}

Current errors are:

Ważność Kod Opis    Projekt Plik    Wiersz  Stan pominięcia
Błąd    C2110   "+": cannot add two pointers    ADBSS

Line 29 and 34.

All your calls of the form

system("string1" + filename.c_str() + "string2");

need to be replaced with

system(("string1" + filename + "string2").c_str());

"string1" is a const char[] literal that decays to const char* when + is applied. filename.c_str() is also a const char* pointer. The compiler issues a diagnostic when you attempt to add two pointers, as that's meaningless.

Writing it the way I have forces + to be the overloaded + operator of the std::string class, which effects a concatenation .

My writing c_str() at the end extracts the data buffer from the anonymous temporary std::string , which is valid for the lifetime of the system function.

A string literal is of type char const[] (which will decay to char const * ). The return type of c_str() is char const * . operator+() is defined for std::string , but not for char pointers. You cannot add two pointers.

You can fix this by setting up the command in a std::string , and then calling system( s.c_str() ) instead of putting together the command in-line:

std::string s( "adb shell screencap -p /sdcard/ADBScreenshooter/" );
s += filename;
s += ".png";

std::system( s.c_str() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM