标签: struct

在类的结构中初始化成员(其构造函数接受参数)

当我尝试使用这种通用格式的代码时,我收到编译器的错误:

在classname.hpp中

#include "otherclass.hpp"
class classname {
public:
    typedef struct {
        otherclass membername(sometype);
    } structname;
    void functionname(structname& exampleData);
}
Run Code Online (Sandbox Code Playgroud)

在main.cpp中,我定义了一个变量exampleData:

classname::structname exampleData;
Run Code Online (Sandbox Code Playgroud)

在classname.cpp中,我有一个使用exampleData的函数,并尝试初始化struct的成员,membername:

void classname::functionname(structname& exampleData){
    sometype a;
    exampleData.membername(a);
}
Run Code Online (Sandbox Code Playgroud)

我从编译器收到错误说明:

未定义的符号:classname::structname::membername(sometype)" classname::functionname(classname::structname&)在classname.o中引用.ld:符号未找到

我认为我的错误在于线

otherclass membername(sometype)
Run Code Online (Sandbox Code Playgroud)

我还以为它可能是:

otherclass membername(sometype& a)
Run Code Online (Sandbox Code Playgroud)

但我收到了同样的错误.

我想知道这是否合法,在构造函数不是()的类中的结构中有一个成员.在main.cpp中,当我声明exampleData时,membername尚未初始化.这是合法的还是我在错误的地方打猎?

c++ struct initialization class

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

C++从结构体到类的变化

我希望将其从结构更改为类,并使用头文件来保存该类。

您建议如何改变它。代码一切正常。那里没有问题。只是一个简单的转换问题。

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct printype
{
    char dots[8][15];
    int unknown15;          //  can have values of 0..127
    string serial11_14;     //  8 characters 00000000...99999999
    int year8;              //  without century, 0..99
    int month7;             //  1..12
    int day6;               //  1..31
    int hour5;              //  0..23
    int minute2;            //  0..59
};                              
int getunknown15(); // prototypes       
string getserial11_14();    
int getyear8();                 
int getmonth7();            
int getday6();              
int gethour5();                 
int getminute2();   
int setunknown15(int);          //------------------------- protos
string setserial11_14(string);  
int setyear8(int);              
int setmonth7(int);             
int …
Run Code Online (Sandbox Code Playgroud)

c++ struct class

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

在C++中初始化一个空数组

我正在用C++编写程序,我需要初始化我创建的struct对象的数组.它看起来像这样:

typedef struct {
float x;
float y;
} vec2;
Run Code Online (Sandbox Code Playgroud)

然后我初始化一个这样的数组:

vec2 hotSpot[1000];
Run Code Online (Sandbox Code Playgroud)

我想当我初始化这样一个数组时,它会完全是空的,但是当我打印sizeof(hotSpot)的值时,它会说8000!

我在某个地方出错了,还是我误解了一些概念?如何将此数组设为空?

c++ arrays struct

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

为什么在结构初始化期间使用点运算符?

可能重复:
点(.)在struct初始化程序中的含义是什么?
[N ... M]在C聚合初始化器中的含义是什么?

struct confd_data_cbs    ssd_shdiag_callback = {
    .callpoint  = show_diag__callpointid_diag_cp,
    .get_object = ssd_common_get_object,
    .get_next   = ssd_common_get_next,
};
Run Code Online (Sandbox Code Playgroud)

.callback,.get_object,.get_next?

c struct

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

Java中的C/C++结构类比?

我在C中有以下代码,而且我对Java知之甚少.

我想知道是否有任何方法可以在Java中创建下面代码中显示的结构.我想我们可以class在Java中使用它,但我在Java Classes中遇到的问题是我无法声明人[10]即这样一个结构的数组.

struct people{
float height;
float weight;
int age;
}people[10];

int main()       // this part of code is just to show how I can access all those elements of struct
{
    int i;
    for(i=0;i<10;i++)
    {
        people[i].height = rand()%7;
        people[i].weight = rand()%80;
        people[i].age = rand()%100;
    }
    for(i=0;i<10;i++)
    {
        printf(" %f %f %d\n",people[i].height,people[i].weight,people[i].age);
    }
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c c++ java struct

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

退出后代码崩溃了?

无论出于何种原因,我的代码崩溃了,但只有在退出程序之后(通过点击"X"),任何人都知道它为什么这样做?这是我的代码.

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <windows.h>
#include <time.h>


#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
using namespace std;

/*
const int AIR = 0;
const int STONE = 1;
const int DIRT = 3;
const int GRASS = 4;
*/


float SCALE = .5;
const int chunkSize = 16;
const int chunkSizeY = 256;
const int blocksTotal = 4;
int chunk[chunkSize][chunkSizeY][chunkSize];
time_t seconds;
int seed = 0;
int quads;

struct block {
  bool Solid;
  int DepthMAX; …
Run Code Online (Sandbox Code Playgroud)

c++ arrays crash struct

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

C++中结构体的qsort

我尝试对结构进行排序,但出现此错误:

\n\n
    error: cannot convert \xe2\x80\x98std::vector<Node>\xe2\x80\x99 to \xe2\x80\x98void*\xe2\x80\x99 for argument \xe2\x80\x981\xe2\x80\x99 to \xe2\x80\x98void qsort(void*, size_t, size_t, __compar_fn_t)\xe2\x80\x99\n  qsort(nodes,nodes.size(), sizeof(Node), dataClustering::compare);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的代码:\n比较函数:

\n\n
int compare(const void * node1, const void * node2){\n    string name1 = ((const struct Node*)node1)->name;\n    string name2 = ((const struct Node*)node2)->name;\n    int start1 = ((const struct Node*)node1)->start;\n    int start2 = ((const struct Node*)node2)->start;\n\n    if(name1 <= name2 && start1 <= start2){\n        return -1;\n    }\n    else if(name1 > name2 && start1 > start2){\n        return 1;\n    }\n    else{\n        return 0;\n …
Run Code Online (Sandbox Code Playgroud)

c++ struct qsort

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

无法将结构转换为具有相同基础字段的不同类型

type Req struct {
    apiVersion       string
    path             string
    resourceEndpoint string
    accessKey        string
    log              *logrus.Entry
    incomingReq      interface{}
    httpClient       lib.HTTPClient
    redisClient      redis.Cmdable
    ctx              context.Context
} 

type TestReq struct {
    Req
}
Run Code Online (Sandbox Code Playgroud)

根据this this question及其答案,我觉得我应该能够做到以下几点:

req := &Req{}
req = TestReq(req)
Run Code Online (Sandbox Code Playgroud)

但是我在 VsCode 中收到此错误:

无法将 req(*Req 类型的变量)转换为 TestReq 编译器(InvalidConversion)

这两个结构体是否具有相同的底层字段?如果是这样,为什么第一个不能转换为第二个?

struct embedding type-conversion go

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

结构组件不相加

我有以下内容:

typedef struct
{
    string city;
    int temp;
}
avg_temp;

avg_temp temps[NUM_CITIES];

void sort_cities(void);

int main(void)
{
    temps[0].city = "Austin";
    temps[0].temp = 97;

    temps[1].city = "Boston";
    temps[1].temp = 82;

    temps[2].city = "Chicago";
    temps[2].temp = 85;

    temps[3].city = "Denver";
    temps[3].temp = 90;

    temps[4].city = "Las Vegas";
    temps[4].temp = 105;

    temps[5].city = "Los Angeles";
    temps[5].temp = 82;

    temps[6].city = "Miami";
    temps[6].temp = 97;

    temps[7].city = "New York";
    temps[7].temp = 85;

    temps[8].city = "Phoenix";
    temps[8].temp = 107;

    temps[9].city = "San Francisco";
    temps[9].temp = …
Run Code Online (Sandbox Code Playgroud)

c memory struct sizeof memory-alignment

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

是否可以重写 typeof() ?

我有一个自定义结构,内部有本机类型,我想让它表现得像本机类型

这是结构:

private struct test
{
    private DateTime Value;

    public test(DateTime value)
    {
       Value = value;
    }           
}
Run Code Online (Sandbox Code Playgroud)

我希望这是真的:

typeof(test) == typeof(DateTime)
Run Code Online (Sandbox Code Playgroud)

c# datetime struct types

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