小编Who*_*aig的帖子

读取变量时出错,无法访问地址 X 处的内存

可能只是 C 新手的另一个愚蠢的指针问题。虽然想不通这个。似乎不知何故我的堆栈帧已损坏。分配似乎无关紧要,但它是一个相当基本的 I/O 练习。尝试通过单次读取读取结构数组(不能使用高级 I/O 函数,例如 fread())。

#include "A2_Phase2.h"

void read_directory(Cdir directory[], int cnt) 
{
    int fd;
    char filename[] = "RandomStructDir.bin";

    fd = open(filename, O_RDONLY, S_IRWXU);
    if (fd < 0)
        perror(strcat(filename, " failed to open."));

    if (read(fd, &(directory[0].code[0]), sizeof(Cdir) * cnt) < 0) {
        perror(strcat(filename, " could not be accessed."));
    }

    close(fd);
}

int binary_search(Cdir directory[], char *key, int l, int r) {

    int mid = (int) r / 2;

    if (strncmp(key, directory[mid].code, 3) < 0)
        return binary_search(directory, key, …
Run Code Online (Sandbox Code Playgroud)

c

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

C++自动从const char到string的隐式转换

好的,我只是第一次学习模板,所以我在玩弄创建我自己的模板类,模仿它的基础类型是一个向量.请记住,对push_back的调用只调用底层向量的push_back方法.

vector<string> sV;
sV.push_back("ha");       //ok: converts from const char[3] to string

Foo<string> fS;
fS.push_back("ha");      //error: const char[3] does not match the argument list
Run Code Online (Sandbox Code Playgroud)

有没有办法解决这个问题?我只是希望我的模板感觉自然就像我正在使用真实的东西一样.


编辑:这基本上是班级的主体

template <typename T> class FooPtr;
template <typename T>
class Foo{
    friend class FooPtr<T>;
public:
    Foo() {data->make_shared(vector<T>); }
#ifdef INITIALIZER_LIST
    Foo(initializer_list<T>);
#endif
    void push_back(T &t) { data->push_back(t); }
    void push_back(T &&t) { data->push_back(move(t)); }
    bool empty() { if (data->size() == 0) return true; }
    FooPtr<T> insert(size_t, T&);
    T& operator[](size_t);
    T& front();
    T& back();
    FooPtr<T> begin() { …
Run Code Online (Sandbox Code Playgroud)

c++ string type-conversion implicit-conversion

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

}之间的区别; 和}在C++中

全新的C++.

在一个项目上进行分配,在一些示例代码中,我发现了以}结尾的方法; 而不是典型的(预期的)}

例如:

CircBuffer::CircBuffer()
{
    cout<<"constructor called\n";
    cout<<"Buffer has " << BufferSize << "elements\n";

    for (int i = 0; i<= BufferSize -1; i++)
    {
        Buffer[i] = 0;
    }

    ReadIn = WriteIn = 0;
    setDelay(0);

}; // <=== HERE
Run Code Online (Sandbox Code Playgroud)

我找不到任何关于为什么要在网上完成的信息.

谢谢,刘易斯

c++ methods

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

错误“此声明没有存储类或类型说明符”

我正在使用 Visual Studio 编写我的第一个应用程序,但我不明白它向我显示的错误。

有两个文件,Session 和 Login。登录使用Session的set和get函数。如下所示,Login 调用“setCurrentLang”,这是 Visual Studio 在 Login.cpp 上显示的消息:“此声明没有存储类或类型说明符”。如果我编译,这就是错误:

“错误 26 错误 C2365:‘setCurrentLang’:重新定义;先前的定义是‘function’(....)\GUI\Login.cpp”。

这是 Session.cpp 文件:

#include "Session.h"
const char* CURRENT_LANG;
void setCurrentLang( char* lang){
    CURRENT_LANG = strdup(lang);
}
const char* getCurrentLang(){
    return CURRENT_LANG;
}
Run Code Online (Sandbox Code Playgroud)

会话.h

#ifndef __SESSION_H__
#define __SESSION_H__

#include <cstring>
#include <stdio.h>

void setCurrentLang( char* lang);
const char* getCurrentLang();

#endif
Run Code Online (Sandbox Code Playgroud)

登录.cpp

#include "Login.h"
#include "../data/Session.h"

setCurrentLang("English"); 
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助!

c c++ visual-studio-2013

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

''中的C++错误:free():指针无效

我已经阅读了类似的问题,但我找不到任何专门解决我的问题的东西(或者我根本不理解其他解决方案)

我正在尝试实现模板Stack类,并且在尝试推送到堆栈时遇到问题.这是我的Stack.cpp:

#ifndef _STACK_H
#define _STACK_H

#include <string>
#include <stdio.h>
#include "Node.cpp"

template<typename T>
class Stack{
    private:
        Node<T>* mHead; 

    public:
        Stack();
        ~Stack();
        void push(T data);
};

template<typename T>
Stack<T>::Stack(){
    mHead = NULL;
}

template<typename T>
Stack<T>::~Stack(){
    delete mHead;
}

template<typename T>
void Stack<T>::push(T data){        // <-- having trouble with this method
    Node<T>* temp = new Node<T>;
    temp->data = data;
    //if head is already empty, just create 1 Node
    if(mHead==NULL){
        printf("if working\n");
        mHead = temp;
    }else{
        printf("else working\n");
        //rearrange Nodes
        temp->next …
Run Code Online (Sandbox Code Playgroud)

c++ free templates abort

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

构造函数和重载的运算符执行顺序

我对这段代码感到困惑.

#include <iostream>
using namespace std;
class A{
    public:
        A(){
            cout << "Constructor" << endl;
        }
        ~A(){
            cout << "Destructor" << endl;
        }
        void operator ~(){
            cout << "\nOverloaded Operator";

        }
};
int main(){
    ~A();
    cout << "\nBefore End";
}
Run Code Online (Sandbox Code Playgroud)

产量

Constructor

Overloaded OperatorDestructor

Before End
Run Code Online (Sandbox Code Playgroud)

我想问一下~A(); 代码构造函数是否A();创建了对象,然后该对象正在调用运算符?如果没有,那么请解释它是如何工作的.谢谢.

c++ constructor

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

在运行时将一维数组视为二维数组

我有一些数据存储在大小为“M”的一维数组中。现在我需要将其视为维度为 NxP 的二维数组,其中 N 和 P 的乘积等于 M。我仅在运行时知道 N 和 P 的值。我怎样才能在C中实现这样的功能?

int array[M]; /* one dimensional array where some data is stored*/
int** newArray; /* the dimension of newArray should be NxP such that we can access the data in 'array' as a two-dimensional array*/
Run Code Online (Sandbox Code Playgroud)

c arrays

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

我可以递归调用每个基类中的方法而无需手动输入base :: Method()吗?

内容

  • 这个问题
  • 我为什么需要它

你好.

这个问题

我正面临一个问题.我有一个A类,它有一个基数B(是多态的).在B类中是方法Print(),它是虚拟的.在A类中也是Print().虚拟.假设我给了一个A类型的对象(或指针),存储在B变量中

B * object = new A(); 
Run Code Online (Sandbox Code Playgroud)

并通过电话

object->Print();
Run Code Online (Sandbox Code Playgroud)

它调用A类中的方法,但我也希望它在B类中调用方法.从技术上讲, 我想为每个孩子调用方法,直到我到达没有孩子的班级 这可以按如下方式完成:

class A
{
   public:
      virtual void Print() const override
      {
          cout << "A" << endl;
      }
};
class B : public A
{
   public:
      virtual void Print() const override
      {
         cout << "B" << endl;
         A::Print();     // i do not want to call it here...
      }
};
Run Code Online (Sandbox Code Playgroud)

问题是我确实不想被迫打电话给

A::Print();
Run Code Online (Sandbox Code Playgroud)

为什么

是的,你可能会问,交易是什么......我有很长的继承链.(假设继承链中有15-20个类).在游戏中,每个人都做一些小事.

让我们说

 class …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism

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

C字符串已反转

我正在写一个简单的c程序,它反转一个字符串,从argv [1]中取出字符串.这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* flip_string(char *string){
    int i = strlen(string);
    int j = 0;

    // Doesn't really matter all I wanted was the same size string for temp.
    char* temp = string;
    puts("This is the original string");
    puts(string);
    puts("This is the \"temp\" string");
    puts(temp);

    for(i; i>=0; i--){
        temp[j] = string[i]
        if (j <= strlen(string)) {
            j++;
        }
    }

    return(temp);
}

int main(int argc, char *argv[]){
    puts(flip_string(argv[1]));
    printf("This is the end of the program\n");
}
Run Code Online (Sandbox Code Playgroud)

基本上就是这样,程序编译并且一切都没有返回到最后的临时字符串(只是空格).在开始时,当它等于字符串时,它会打印temp.此外,如果我在for循环中使用temp的字符printf执行字符,则打印正确的临时字符串即字符串 …

c string

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

调试断言失败 c &gt;= -1 &amp;&amp; c &lt;= 255

我很难理解我的代码的运行时错误。这是我的班级作业,我认为很容易,但发生了一些奇怪的事情。我在下面发布了我的代码。

这个作业的提示是创建一个程序,要求用户输入一些单词,然后它应该输出有多少个单词。我有一个朋友运行该程序并且它有效,但是,每当我尝试运行它时,我都会收到此运行时错误。[ https://i.stack.imgur.com/Vhqbe.jpg ]

请记住,我是 C++ 和一般编程的初学者,因此我无法理解非常技术性的答复。预先感谢您的帮助。

#include <string>
#include <iostream>
#include <cstring>

using namespace std;

int wordCounter(char *);

int main()
{
    char *stringArray = nullptr;
    stringArray = new char[120];

    cout << "Please enter a saying or a phrase that has more than one word: " << endl;
    cin.getline(stringArray, 120);

    int words = wordCounter(stringArray);
    cout << "Your statement contains " << words << " words." << endl;

    system("pause");
    return 0;
}

int wordCounter(char *stringTest)
{
    char characterToTest;
    int numberOfWords = …
Run Code Online (Sandbox Code Playgroud)

c++ c-strings visual-studio

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