相关疑难解决方法(0)

Windows系统编程OpenFile功能

Windows中的OpenFile功能文档位于此处.而我正在尝试这样做:

#include "Tchar.h"
#include <windows.h>
int main(int argc, TCHAR *argv[]){

    LPOFSTRUCT _buffer;
    HFILE _hfile_ = OpenFile("E:\\mozunit.txt", _buffer, OF_READ);
    LPVOID _buffer_read;
    LPDWORD _bytes_read;
    bool flag = ReadFile(_buffer, _buffer_read, 5, _bytes_read, NULL);
    CloseHandle(_buffer);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,当我运行这个时,我得到一个我没有初始化的错误_buffer.所以要反击我这样初始化_buffer:

LPOFSTRUCT _buffer = NULL;
Run Code Online (Sandbox Code Playgroud)

这给了我一个访问冲突错误.这是为什么?

c++ windows

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

错误C2678:二进制'==':找不到带左手操作数的运算符

我无法弄清楚为什么我得到这个错误但是同一个类在VS15下完美运行现在我正在使用VS12,它是一个简单的Winsock2实现,

int Net::createServer(int port, int protocol) 
{
    int status;

    // ----- Initialize network stuff -----
    status = initialize(port, protocol);
    if (status != NET_OK)
        return status;

    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);    // listen on all addresses

    // bind socket
    if (bind(sock, (SOCKADDR *)&localAddr, sizeof(localAddr)) == SOCKET_ERROR)
    {
        status = WSAGetLastError();          // get detailed error
        return ((status << 16) + NET_BIND_FAILED);
    }
    bound = true;
    mode = SERVER;

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

问题来自这里

if (bind(sock, (SOCKADDR *)&localAddr, sizeof(localAddr)) == SOCKET_ERROR)
Run Code Online (Sandbox Code Playgroud)

控制台日志:

error C2678: binary '==' …
Run Code Online (Sandbox Code Playgroud)

c++ operators winsock2

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

C++:错误:预期';' 在声明列表的末尾(用clang ++ 3.4编译)

我试着写一个非常简单的进度条类.我将所有内容放入progressBar.h中,并创建了一个最小的测试用例.我在Ubuntu 14.04上使用clang ++ 3.4运行它.我包括-std = c ++ 11标志.测试用例创建了一个新的ProgressBar对象并调用成员函数displayProgress(double).我包括在ProgressBar.h中.

这是我的代码:

#include <ostream>
#include <string>

class ProgressBar
{
private:
    std::ostream& out;
    int width;

public:
    ProgressBar(std::ostream& out, int _width = 80)?out(out) {
        width = _width - 9;
    }

    void displayProgress(double percentage) {
        out << "\r[" << fixed << setprecision(2) << percentage * 100 << "%]";
        for (int i = 0; i < width * percentage; ++i) out << "=";
        if (percentage != 1) out << ">";
        else out << "O";
    }
};
Run Code Online (Sandbox Code Playgroud)

这是错误消息: …

c++ c++11 clang++

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

operator <<必须只取一个参数

我写的类有错误,我不明白是什么问题.

这是工人阶级:

#ifndef __Worker__
#define __Worker__

#include <iostream>
#include <string> 

class Worker{
    protected:
        std::string name;
        int id;
        int salary;

    public:
        Worker(const std::string& name, int id, int salary);
        Worker(const Worker& worker );
        friend std::ostream& operator<<(std::ostream& os, const Worker& w);
        int getWorkerID(){
            return this->id;
        }
        int getsSalary(){
            return this->salary;
        }
        std::string getname(){
            return this->name;
        }

};
#endif
Run Code Online (Sandbox Code Playgroud)

这是这个类的实现:

#include "Worker.hpp"

Worker::Worker(const std::string &names, int ids, int salarys) :
name(names), id(ids), salary(salarys)
{
}

Worker::Worker(const Worker &worker): 
name(worker.name), id(worker.id), salary(worker.salary)
{

}


std::ostream& …
Run Code Online (Sandbox Code Playgroud)

c++

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

标准库实现中四个双下划线的原因

标准库(C 或 C++)实现是否存在任何技术原因(IMO 滥用),强调它们的做法(= 用两个下划线作为所有内容的前缀 + 添加尾部下划线以表示变量是成员变量)?

我明白了/.*__.*/并且 /_[A-Z].*/(<= regexes) 是由实现保留的。但这不是应该指编译器的实现而不是(标准)库吗?

标准库在选择内部名称方面不能像其他库一样吗?

c c++ libc libc++

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

`char _3 ='c'`在C和C++中有效吗?

考虑到,我已将变量名称声明为带下划线的数字.

#include <stdio.h>

int main()
{
   char _3 = 'c';
   printf("%c\n",_3);
}
Run Code Online (Sandbox Code Playgroud)

我想知道,它在C和C++中运行良好.那么,它有效吗?

c c++ variables identifier

-2
推荐指数
2
解决办法
294
查看次数

g ++编译器"重新定义...以前定义过"

我知道有类似的问题,但在我的案例中没有一个工作.嗨,我找不到为什么我有这个问题.这是我的个人文件:

#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H

#include <vector>
#include <stdlib.h>
#include <time.h> 
#include <iostream>


using namespace std;

class Individual{
    private:
        vector<unsigned int> chromosome;
        unsigned int n_genes;
        unsigned int N_colours_used = 0;
        unsigned int fitness = 0;

    public:
        Individual(unsigned int ngenes){};

};

#endif
Run Code Online (Sandbox Code Playgroud)

这是我的个人.cpp文件:

#include "individual.h"


Individual :: Individual(unsigned int ngenes){
    cout << "something" << endl;
}
Run Code Online (Sandbox Code Playgroud)

错误看起来像这样

src/individual.cpp:4:1: error: redefinition of ‘Individual::Individual(unsigned int)’
Individual :: Individual(unsigned int ngenes){
 ^
In file included from src/individual.cpp:1:0:
include/individual.h:24:13: note: ‘Individual::Individual(unsigned int)’ previously defined here …
Run Code Online (Sandbox Code Playgroud)

c++ gcc makefile g++ c++11

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

包含带有浮点数和另一个整数向量的其他向量的向量

我正在尝试创建一个向量,其中包含带有浮点数和其他整数向量的其他向量。预期结果:

{{5.4 {1, 5, 4, 6, 9},
4.8, {3, 6, 7, 8, 4},
7.3 , {9, 12, 1, 0, 4}}
Run Code Online (Sandbox Code Playgroud)

这是我的尝试之一:

#include <iostream>
#include <vector>


using namespace std;


void _cost(){
    vector<float, vector<int>> result;

    cout<<"This work";
}

int main(){

    _cost();


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

在我所有的尝试中,我收到以下错误:

In file included from /usr/include/c++/7/ext/alloc_traits.h:36:0,
                 from /usr/include/c++/7/bits/basic_string.h:40,
                 from /usr/include/c++/7/string:52,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from prueba_dela_prueba.cpp:1:
/usr/include/c++/7/bits/alloc_traits.h: In instantiation of ‘static void std::allocator_traits<_Alloc>::deallocate(_Alloc&, std::allocator_traits<_Alloc>::pointer, std::allocator_traits<_Alloc>::size_type) [with _Alloc = std::vector<float, std::allocator<int> >; std::allocator_traits<_Alloc>::pointer …
Run Code Online (Sandbox Code Playgroud)

c++ stl vector

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

我可以在::?中使用#define指令吗?为什么它不起作用?

我写了一个B类作为A的子类来扩展它.现在我想改变一切

 a = A::create(); 
Run Code Online (Sandbox Code Playgroud)

 a = (A*)B::create(); 
Run Code Online (Sandbox Code Playgroud)

一切正常.但我不想在创建A对象的任何地方手动更改代码.所以我尝试了以下#define指令

#define A::create (A*)B::create
Run Code Online (Sandbox Code Playgroud)

但它根本不起作用.我不知道问题是什么.有帮助吗?我可以在::?中使用#define指令吗?为什么它不起作用?

c++

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

不允许使用不完整的类型(C ++ VSCODE)

我只是a使用容器类创建了一个数组。但是,VScode的IntelliSense显示错误。这是选择排序的实现。

![IntelliSense错误

c_cpp_properties.json文件的内容如下

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.16299.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}
Run Code Online (Sandbox Code Playgroud)

该代码将编译并成功运行。如何解决错误的IntelliSense错误?

c++ windows visual-studio-code

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

有没有一种方法可以让#define普通文字成为C++的基本运算符?

问题描述

我参加竞争性节目。鉴于我迫切需要一个基本的模板来记忆,我非常使用宏。

例如,我使用

#define large long long
large value;
Run Code Online (Sandbox Code Playgroud)

不过,有两个运算符让我很恼火:<<以及>>as of 输入和输出。

期待

我想看到类似的东西

#include <iostream>
using namespace std;
#define and <<
#define with >>
#define print cout
#define input cin

int main() {
    long long value1, value2;
    input value1 with value2;
    print value1 and value2;
}
Run Code Online (Sandbox Code Playgroud)

我希望这个程序能够回显用户输入的两个整数值。

问题

但是,我不断收到一条错误消息,指出Cannot #define something as an operator. 我该如何解决这个问题?如果我不能,有替代品吗#define and <<?我非常感谢您的回复。

c++

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

在C/C++中将变量名定义为__00000001是否有任何优点

将变量名称定义为__00000001,__00000002等有什么好处吗?

例:

int __00000001, __00000002 = 0;
for (__00000001 = 0; __00000001 < 10; __00000001++) {
    __00000002 = __00000002 + __00000001?
}
...
Run Code Online (Sandbox Code Playgroud)

更新:几年前我的一个编程课程中提到了这一点,我记得教授说使用它有一些优点.但是,我不记得任何更多的信息.也许我错了.

c c++ variable-names

-4
推荐指数
1
解决办法
103
查看次数