我正在解决以下 Leetcode 问题:
https://leetcode.com/problems/find-if-path-exists-in-graph/
我收到以下错误:
Line 1034: Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int>>' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9
Run Code Online (Sandbox Code Playgroud)
我的方法的代码:
class Solution {
public:
void dfs(int *visited,int node,vector<vector<int>>&adj)
{
visited[node]=1;
for(auto child:adj[node])
{
if(visited[child]==-1)
dfs(visited,child,adj);
}
}
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
vector<vector<int>>adj;
for(auto it:edges)
{
adj[it[0]].push_back(it[1]);
}
int visited[n];
for(int i=0;i<n;i++)
visited[i]=-1;
dfs(visited,source,adj);
return visited[destination]==1;
}
};
Run Code Online (Sandbox Code Playgroud)
几乎每个图形问题我都会遇到此错误。有人可以指出错误吗?
我正在编写两个类来处理简单的拍卖.我有一个准备好工作的班级,处理单次拍卖的操作,现在我正在编写另一个类似拍卖行的班级,以跟踪所有可用的拍卖.在测试课程的以下部分时:
import java.util.ArrayList;
public class AuctionHouse {
private ArrayList<DutchAuction> huutokaupat;
public AuctionHouse() {
}
public void addAuction(DutchAuction newAuction) {
huutokaupat.add(newAuction);
}
}
Run Code Online (Sandbox Code Playgroud)
在一个带有以下代码的main方法中("kauppa"是一个经过测试和工作的对象变量):
AuctionHouse talo = new AuctionHouse();
talo.addAuction(kauppa);
Run Code Online (Sandbox Code Playgroud)
我明白了:
在ope.auction.dutch.DutchAuctionTest.main(DutchAuctionTest.java:54)的ope.auction.dutch.AuctionHouse.addAuction(AuctionHouse.java:13)中的线程"main"java.lang.NullPointerException中的异常
我该如何解决这个问题?
写此代码之前我的目标是只是练习和学习更多关于C++.
该代码由一个类球组成,该球具有球的属性,如颜色,大小,重量以及球的"品牌"和价格.
#include<iostream>
#include<string>
using namespace std;
class ball
{
private:
int price; //Price of a ball in rupees .
enum colour { red , green , blue } colour; // Colour of the ball .
string brand; // Brand of the ball REEBOK ADIDAS etcetera .
float size; // Diameter of the ball .
enum weight { light , medium , heavy }weight; // Qualitative weight .
public:
ball();
void get_price();
void get_colour(); …Run Code Online (Sandbox Code Playgroud) 我知道C,但我不擅长C++.
以下代码将崩溃(在getval()中,使用引用作为参数是ok).并且*p在第一次cout陈述之后改变了价值.看起来存在一些超出内存的覆盖.
我的问题是它崩溃的原因(或者为什么它的价值发生了变化).这是对象的"按价值呼叫",所以它应该工作吗?
class myclass {
int *p;
public:
myclass(int i);
~myclass() { delete p; }
int getval(myclass o);
};
myclass::myclass(int i)
{
p = new int;
if (!p) {
cout << "Allocation error\n";
exit(1);
}
*p = i;
}
int myclass::getval(myclass o)
{
return *o.p;
}
int main()
{
myclass a(1), b(2);
cout << a.getval(a) << " " << a.getval(b) << endl;
cout << b.getval(a) << " " << b.getval(b) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这只是一个基本的打印句子数组字符串.我是c ++的新手,只使用过JAVA和类似的语言.尝试通过遍历每种不同的排序算法和数据结构来学习它.
但在我开始之前,只测试我的字符串数组会给我一个错误.我不知道为什么它会给我一个错误.实际编译运行良好并打印意图内容,但如果您正在调试它则会崩溃并出现错误.任何人都可以向我解释为什么会这样.尝试size()并length()从C++库,但不得不使用sizeof()
//BubbleSort.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
string something[14];
something[0] = "Kate";
something[1] = "likes";
something[2] = "lots";
something[3] = "of";
something[4] = "cake";
something[5] = "in";
something[6] = "her";
something[7] = "mouth";
something[8] = "and";
something[9] = "will";
something[10] = "pay";
something[11] = "a";
something[12] = "lot";
something[13] = "lol";
int some = sizeof(something);
some--;
for (int i = 0; i < some; i++)
{
cout …Run Code Online (Sandbox Code Playgroud) 我正在编写一个配置文件解析器,并在我的Config.php文件中有一个名为getVals()的函数,但显然当我在测试中调用它时会抛出"未定义函数"错误.
config.php文件
<?php
require_once '../extlib/pear/Config/Lite.php';
class Config {
private $config;
function __construct($conf) {
$this->config = new Config_Lite();
echo "calling open...<br>";
$this->open($conf);
echo "open done...<br>";
}
function open($cfile) {
if (file_exists($cfile)) {
$this->config->read($cfile);
} else {
file_put_contents($cfile, "");
$this->open($cfile);
}
}
function getVals() {
return $this->config;
}
function setVals($group, $key, $value) {
$this->config->set($group, $key, $value);
}
function save() {
$this->config->save();
}
}
?>
Run Code Online (Sandbox Code Playgroud)
cfgtest.php中的测试类
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../util/Config.php';
$cfile = "../../test.cfg";
$cfg = new Config($cfile);
if (is_null($cfg)) {
echo …Run Code Online (Sandbox Code Playgroud) 我是C++的新手,我一直在玩指针和类.我遇到了一个问题,到目前为止我还没有找到解决方案:
RAII.exe中0x77F87508(msvcr110d.dll)的未处理异常:0xC0000005:访问冲突读取位置0xCCCCCCC0.
它似乎与我访问一个我无法访问的指针有关.
Main.cpp的:
#include <memory>
#include <iostream>
#include "Example.hpp"
void example()
{
Example e;
}
int main()
{
example();
std::cout << "Press any key to exit";
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Example.cpp:
#include "Example.hpp"
Example::Example()
{
m_a = new int(1);
m_b = new int(2);
m_b = new int(3);
}
Example::~Example()
{
delete m_a;
delete m_b;
delete m_c;
}
Run Code Online (Sandbox Code Playgroud)
Example.hpp:
#ifndef _EXAMPLE_HPP_
#define _EXAMPLE_HPP_
#include <memory>
#include <iostream>
class Example
{
private:
int *m_a;
int *m_b;
int *m_c;
public:
Example();
~Example(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个创建一副牌的小程序.所有代码都编译,但是当我尝试运行测试器类时,它会说:
线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:52
at cardDeck.giveValue(cardDeck.java:23)
at CardTest.main(CardTest.java:6)
我只使用java几周,所以我不确切地知道问题出在哪里,或者如何解决它.我很感激任何帮助.
卡类:
public class Card {
private int value;
private String name;
private String suit;
public void setValue(int v){
value = v;
}
public int getValue(){
return value;
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
public void setSuit(String s){
suit = s;
}
public String getSuit(){
return suit;
}
}
Run Code Online (Sandbox Code Playgroud)
卡片类:
public class cardDeck {
int [] values = {2, 3, 4, 5, 6, 7, 8, …Run Code Online (Sandbox Code Playgroud) 所以我正在编写这个程序,它使用open()创建读取文件.当我尝试运行程序时给出了错误:ValueError:关闭文件的I/O操作(第18行).
在我在这个主题上看到的每个问题中,问题总是与缩进有关.IE尝试操作未在同一循环中打开的文件.我的蟒蛇有点生疏,但缩进对我来说很好看.
我希望有人可以快速查看,让我知道缩进是否错误,或者如果其他东西可能导致错误?代码(注释错误):
sourceFile = 'test.html'
serviceTarget = "Plumbers & HVAC Experts"
cityTarget = "NJ"
services = {"Plumbers", "Air Conditioning Experts", "Drain Cleaning Experts"}
cities = {"Westfield", "Scotch Plains", "Clark"}
serviceNames = {"Plumbers":"plumbers", "Air Conditioning Experts":"ac", "Drain Cleaning Experts":"drain"}
totalPages = len(services)*len(cities)
for serviceRep in services:
for cityRep in cities:
outFileName = cityRep + " " + serviceNames[serviceRep] + ".html"
outFileName = outFileName.replace(" ", "_");
print("Writing " + outFileName + "...")
infile = open(sourceFile)
outfile = open(outFileName, 'w')
for line …Run Code Online (Sandbox Code Playgroud) 我首先得到两个运行时错误({"序列包含多个元素"} ==> System.InvalidOperationException)当它出现不止一次时以及当我想删除我在文本框中写的内容时该怎么办? ?
这是我的代码
private AutoCompleteStringCollection GetLinqDataSourceForString(string p)
{
var c = new AutoCompleteStringCollection();
c.Add( Getauto(p));
return c;
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
txtSearch.AutoCompleteCustomSource = GetLinqDataSourceForString(txtSearch.Text);
}
Run Code Online (Sandbox Code Playgroud)
而这一个是在另一层从数据库获取信息
public string AutoUpdate(string _search)
{
using (var context = new Phone_BookEntities1())
{
var c = (from d in context.Cantacts
where d.Cantact1.StartsWith(_search)
select d.Cantact1).SingleOrDefault();
return c;
}
}
Run Code Online (Sandbox Code Playgroud)