我知道以前有人问过这个问题,我已经尝试了所有可能的答案,但仍然没有任何帮助。
但是,再次刷新问题并详细说明我的问题。我实际上是在尝试将一个简单的文件包含到 main.go 文件中。我的文件夹结构和其余信息如下:
\src\
Multi-file\
lib\Car.go
main.go
Run Code Online (Sandbox Code Playgroud)
货物
package main
type Car struct {
numberOfDoors int
cylinders int
}
Run Code Online (Sandbox Code Playgroud)
main.go
package main
import (
"fmt"
)
func main() {
c := Car{4,6}
fmt.Println(c)
}
Run Code Online (Sandbox Code Playgroud)
当我编译 main.go 时,出现以下错误
# command-line-arguments
.\main.go:8: undefined: Car
Run Code Online (Sandbox Code Playgroud)
这是我的go env详细信息:
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\xampp\htdocs\golang
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set …Run Code Online (Sandbox Code Playgroud) 我有与这个几乎相同的问题: 从结构“无效读/写”中的指针获取数据
但是当我尝试遵循这些建议时,我仍然对大小进行了相同的无效读取。
我的结构是这样的:
typedef struct{
int lenght;
int max_lenght;
int extract;
int inserting;
void** structure;
} queue_t;
Run Code Online (Sandbox Code Playgroud)
我的循环缓冲区代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "queue.h"
/* creates a new queue with a given size */
queue_t* create_queue(int capacity){
queue_t* queue = malloc (sizeof(queue_t));
queue->lenght = 0;
queue -> max_lenght = capacity;
queue -> extract = 0;
queue -> inserting = 0;
queue -> structure = malloc(sizeof(void*) * capacity);
return queue;
}
/* deletes the queue and all …Run Code Online (Sandbox Code Playgroud) 让我们说在第三方库中我们有一个接口和一个实现这个接口的结构.我们还假设有一个函数将ParentInterface作为参数,它对不同类型具有不同的行为.
type ParentInterface interface {
SomeMethod()
}
type ParentStruct struct {
...
}
func SomeFunction(p ParentInterface) {
switch x := p.Type {
case ParentStruct:
return 1
}
return 0
}
Run Code Online (Sandbox Code Playgroud)
在我们的代码中,我们想要使用这个接口,但是使用我们的增强行为,所以我们将它嵌入到我们自己的结构中.编译器实际上允许我们ParentInterface直接在我的struct上调用函数:
type MyStruct struct {
ParentInterface
}
parentStruct := ParentStruct{...}
myStruct := MyStruct{parentStruct}
parentStruct.SomeMethod() // Compiler OK.
myStruct.SomeMethod() // Compiler OK. Result is same. Great.
SomeFunction(parentStruct) // Compiler OK. Result is 1.
SomeFunction(myStruct.ParentInterface) // Compiler OK. Result is 1.
SomeFunction(myStruct) // Compiler OK. Result is 0. (!)
Run Code Online (Sandbox Code Playgroud)
最后一例不是问题吗?我不止一次遇到过这种错误.因为我愉快地使用 …
我按照本指南设置了一个基本套接字服务器:https: //www.geeksforgeeks.org/socket-programming-cc/
所以,我知道当你使用关键字struct来定义数据结构时,但正如你在示例中所看到的,它使用结构关键字创建了sockaddr_in的实例,但它不是用于创建/定义结构,而是用于创建一个实例.
所以我想知道为什么在本指南中定义一个sockaddr_in结构,他之前放置'structure'关键字,我没有尝试它,它正确编译.
背后有什么理由吗?
struct Dates
{
int day;
int month;
int year;
}accountinfo[3];
struct Accounts
{
string name,lastname;
int number;
float balance;
}account[3];
void sortduetoaccnumbers()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (account[j].number>account[j+1].number)
{
//swap
}
}
}
}
void sortduetodates()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (accountinfo[j].year>accountinfo[j+1].year)
{
//swap
}
else if (accountinfo[j].year==accountinfo[j+1].year)
{
if (accountinfo[j].month>accountinfo[j+1].month)
{
//swap
}
else if (accountinfo[j].month==accountinfo[j+1].month)
{
if (accountinfo[j].day>accountinfo[j+1].day)
{
//swap
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法使用排序算法对这些帐户进行排序.如果我输入它会崩溃.cmd突然停止并完成程序.
我进入了一个评论行,交换函数必须去.所以你可以分析代码.
除此之外,其他所有功能都在工作.我陷入了困境.
我试图在c#中创建一个ufloat类/结构.这对我来说更具挑战性,但可以帮助我控制代码中的一些值.在尝试了几种方法之后,我终于找到了一个似乎有用的方法:
public struct ufloat
{
public float Value
{
get{ return value; }
set
{
if(value < 0)
{
this.value = 0;
}
else
{
this.value = Math.Abs(value);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,我希望它表现得像一个典型的基本类型:
ufloat n = 5;
n += 1;
Run Code Online (Sandbox Code Playgroud)
经过一番思考后,我试图重载'='运算符,但这是不可能的.现在我没有想法.这就是为什么我问,你怎么能改变这个:
ufloat x; x.value = 1;
Run Code Online (Sandbox Code Playgroud)
对此:
ufloat x = 0; x = 1;
Run Code Online (Sandbox Code Playgroud)
?
(对不起,如果我失去了一些非常简单的东西,但我是一个自学成才的"程序员",我对c#很新.我最初学习了c ++,而从低级到高级对我来说并不容易.)
在我的第一次面试中,我被问到Swift结构和Objective-C结构之间有什么区别。
谁能深入解释这种差异?任何帮助,将不胜感激。
我有一个特定的问题,我需要一个可以采用一个或另一个结构的值的变量。我用一个具有接口值的用户字段创建一个userResp结构,但是如果我添加一个函数来返回名为Password的用户的子属性,则会返回错误。user的值将是具有Password属性的结构。
结构:
type userResp struct {
user interface{}
}
Run Code Online (Sandbox Code Playgroud)
功能
func (ur *userResp) password() string {
return ur.user.Password
}
Run Code Online (Sandbox Code Playgroud)
我得到了,ur.user.Password undefined (type interface {} is interface with no methods)但是界面user可以是带有Password字段的Admin或User结构。
任何想法如何执行此操作,我需要使用用户或admin的结构并将其返回。
我不能使用2个函数,因为两种情况下的逻辑都是相同的。我需要用户或管理员的整个结构
在尝试解析时间序列数据时,我发现 JSON 数据中的一个关键字段是时间戳(显然是字符串格式)。但是事先为相同的结构创建一个结构是不可能的,因为无论如何我都不知道时间戳字符串。
这是 JSON 的样子:
"Time Series (5min)": {
"2020-01-17 16:00:00": {
"1. open": "167.2000",
"2. high": "167.3400",
"3. low": "167.0100",
"4. close": "167.0500",
"5. volume": "1646699"
},
"2020-01-17 15:55:00": {
"1. open": "166.9000",
"2. high": "167.1600",
"3. low": "166.8500",
"4. close": "167.1500",
"5. volume": "622999"
},
"2020-01-17 15:50:00": {
"1. open": "166.7241",
"2. high": "166.9200",
"3. low": "166.7200",
"4. close": "166.8999",
"5. volume": "271723"
}
}
Run Code Online (Sandbox Code Playgroud)
有些人的结构可能如下所示:
type TIMESTAMP struct {
Open string `json:"1. open"`
High string `json:"2. …Run Code Online (Sandbox Code Playgroud) 我已经看到结构的第一个地址同时是该结构的第一个成员的第一个地址。现在我想了解的是,为什么我总是需要双指针在结构中移动:
#include <stdio.h>
#include <stdlib.h>
struct foo
{
char *s;
char *q;
};
int main()
{
struct foo *p = malloc(sizeof(struct foo));
char ar[] = "abcd\n";
char ar2[] = "efgh\n";
*(char**)p = ar;
*(char**)((char**)p+1) = ar2; //here pointer arithmetic (char**)p+1
printf("%s\n",p->q);
}
Run Code Online (Sandbox Code Playgroud)
问题是,为什么我需要char**而不是 simple char*?我在汇编程序中看到的是在简单的情况下char*,算术会表现得像正常一样char。也就是说 -> 的表达式(char*)p+1会将地址p仅移动一个字节(而不是8地址为 8 个字节长)。但是类型char* 是地址,所以我不明白为什么算术的行为类似于取消引用类型(纯字符 -> 一个字节)。
所以对我来说唯一的解决方案是添加另一个间接char**,其中指针算术神奇地8作为大小。那么为什么在结构中需要这种奇怪的转换呢?