我想要一个包含引用的对象,并将该对象放入向量中...
我必须在我想要推送到矢量的任何对象中使用智能指针而不是成员引用吗?这就是我想要做的:
#include <string>
#include <vector>
using namespace std;
class MyClass {
public:
MyClass(const string& str_ref); //constructor
MyClass(const MyClass& mc); //copy constructor
private:
string& my_str;
};
MyClass::MyClass(const string& str_ref) :
my_str(str_ref)
{}
MyClass::MyClass(const MyClass& mc) :
my_str(mc.my_str)
{}
int main() {
//create obj and pass in reference
string s = "hello";
MyClass my_cls(s);
//put into vector
vector<MyClass> vec;
vec.push_back(my_cls);
return 0;
}
//Throws Error
//ref.cpp:6:7: error: non-static reference member ‘std::string& MyClass::my_str’, can’t use default assignment operator
Run Code Online (Sandbox Code Playgroud)
但是它说我需要实现我自己的operator =(),因为默认生成的一个是无效的,但当然,没有合法的方法可以这样做......
#include <string> …Run Code Online (Sandbox Code Playgroud) 我正在执行以下代码块,并且编译器抱怨未分配的局部变量,并且可以使用一些帮助来识别出什么.
while (rsData.Read())
{
if (rsData["TYPE"] != DBNull.Value)
strType = rsData["TYPE"].ToString().Trim();
if (strType == "01")
{
if (rsData["Text"] != DBNull.Value)
strwho = rsData["Text"].ToString();
if ((strwho.Length < 10 || (strwho.IndexOf("NULL") > 1)))
strwho = "";
}
else if (strType == "07")
{
if (rsData["Text"] != DBNull.Value)
strmetades = rsData["Text"].ToString();
if ((strmetades.Length < 10 || (strmetades.IndexOf("NULL") > 1)))
strmetades = "";
}
Run Code Online (Sandbox Code Playgroud)
它抱怨所有'if(strType =="01")'行,我不确定是什么.我曾想过为此使用一个开关,但似乎也遇到了同样的问题.
有任何想法吗?
我希望有一个所有间隔的总和,但我写这个代码我有一个错误说明:使用未分配的局部变量total?
enter TimeSpan total;
foreach (var grp in query)
{
TimeSpan interval = TimeSpan.FromMinutes(grp.Minuut);
TimeSpan intervalH = TimeSpan.FromHours(grp.Sum);
interval = interval + intervalH;
total += interval;
string timeInterval = interval.ToString();
dataGridView2.Rows.Add(i++, grp.Id, grp.Sum, grp.Minuut,timeInterval);
}
Run Code Online (Sandbox Code Playgroud) 四处搜索后,我似乎无法找到为什么 C# 编译器抱怨本地变量dteDest 在行中未分配
if (dteSrc == dteDest) {
Run Code Online (Sandbox Code Playgroud)
如果我更换线路,错误就会消失
DateTime dteSrc, dteDest;
Run Code Online (Sandbox Code Playgroud)
和
DateTime dteSrc, dteDest = DateTime.MinValue;
Run Code Online (Sandbox Code Playgroud)
据我所知,如果 dteDest 没有被 DateTime.TryParse 初始化,它是一个 out 参数,那么代码永远不会到达比较行。
我的逻辑是:
void StrangeLogic(object srcData, object currentDataObj) {
DateTime dteSrc, dteDest;
bool booHaveNewDate = DateTime.TryParse(srcData.ToString(), out dteSrc);
bool booHaveOrigDate = (currentDataObj != null)
&& …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法,在为方程中的变量赋值之前,将多个数学函数加在一起.
我这样做是因为我需要优化我的代码,并且我希望每次都为变量分配不同的值.我想要做的一个例子:
f(x, y) = x + 2y
g(x, y) = 3x - y
增加f(x, y) + g(x, y)得到h(x, y),所以f(x, y) + g(x, y) = h(x, y) = 4x + y
现在我有h(x, y),我需要多个值h(x, y)
x = 4; y = 3, h(x, y) = 19
x = 1, y = 0, h(x, y) = 4
Run Code Online (Sandbox Code Playgroud)
等等
这可能吗?我试图将它们创建为字符串,添加字符串,然后删除引号以评估总和,但这不起作用.我试图以这种方式做我的方法,因为我想优化我的代码.如果我能够在评估它之前创建我的最终函数(h(x, y)在这种情况下就是这样),那将非常有帮助.
编辑:我正在添加(e**(x + y)),所以使用矩阵的线性解决方案不起作用:/
我有一段迭代XML属性的代码:
string groupName;
do
{
switch (/* ... */)
{
case "NAME":
groupName = thisNavigator.Value;
break;
case "HINT":
// use groupName
Run Code Online (Sandbox Code Playgroud)
但是这样我得到了使用未分配变量的错误.如果我为groupName分配了一些东西,那么我就无法改变它,因为这就是字符串在C#中的工作方式.任何解决方法?
我刚刚开始使用C#,我已经坚持了两个星期这个问题.我有一个主窗体,从类和子类获取值.我的问题是当我尝试创建一个CorporateClass的对象时,VB告诉我我的两个变量(CarSizeInteger和DiscountInteger)是未分配的.我的问题是为什么.我在程序的早期实现了它们.救命!我绝望地被卡住了!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EX02_CarRentals
{
public partial class RentalForm : Form
{
public RentalForm()
{
InitializeComponent();
}
private void CloseButton_Click(object sender, EventArgs e)
{
Close();
}
private void CalculateButton_Click(object sender, EventArgs e)
{
int DaysInteger, BeginningOdometerInteger, EndingOdometerInteger, CarSizeInteger, DiscountInteger;
if (LicenseTextBox.Text != "")
if (CompactRadioButton.Checked || MidSizeRadioButton.Checked || LuxuryRadioButton.Checked)
{
int.TryParse(DaysRentedTextBox.Text, out DaysInteger);
int.TryParse(BeginningOdometerTextBox.Text, out BeginningOdometerInteger);
if (BeginningOdometerInteger > 0)
{
int.TryParse(EndingOdometerTextBox.Text, out EndingOdometerInteger);
if …Run Code Online (Sandbox Code Playgroud) 我试图读取.csv文件,进行一些格式化,将每一行拆分为其列数据,并将新的分离列数据数组添加到数组列表中.然后我想以不同的方式订购列表.目前只是按用户名按字母顺序升序.
这是我到目前为止所尝试的:
// create list for storing arrays
List<string[]> users;
string[] lineData;
string line;
// read in stremreader
System.IO.StreamReader file = new System.IO.StreamReader("dcpmc_whitelist.csv");
// loop through each line and remove any speech marks
while((line = file.ReadLine()) != null)
{
// remove speech marks from each line
line = line.Replace("\"", "");
// split line into each column
lineData = line.Split(';');
// add each element of split array to the list of arrays
users.Add(lineData);
}
IOrderedEnumerable<String[]> usersByUsername = users.OrderBy(user => user[1]);
Console.WriteLine(usersByUsername); …Run Code Online (Sandbox Code Playgroud) c# sorting console-application visual-studio-2010 unassigned-variable
我有一些问题,为什么C#不喜欢这coordinates[j]部分以及我能做些什么.
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\sp\Dropbox\ProjectEuler\102\p102_triangles.txt");
string[] coordinates_str;
double[] coordinates; //Contains the coordinates for each line A1(x,y), A2(x,y), A3(x,y)
long ln = lines.Length;
Console.WriteLine("Length: " + ln.ToString());
for (int i = 0; i < ln; i++)
{
Console.Write(i);
Console.Write(lines[i]);
coordinates_str = lines[i].Split(',');
for (int j = 0; j < 6; j++)
{
coordinates[j] = Convert.ToDouble(coordinates_str[j]);
}
}
Run Code Online (Sandbox Code Playgroud) 用户提供"从"和"到"月份以生成报告(例如,从1个月回到13个月;如果他们在2016年2月15日选择此项,则返回值为2015年1月1日, 2016年1月1日).
我想允许用户从"from"或"to"组合框中选择最远的后面或最近的月份.我只想用最远的时间作为"从"和最接近的时间作为"to"以避免他们的混淆(他们可以做任何看起来很自然的事情).
所以我从这段代码开始:
int fromMonths = Convert.ToInt32(comboBoxProduceUsageFrom.Text);
DateTime RptParamsNominalFromDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(fromMonths, nextGenerateAndSendDate);
int toMonths = Convert.ToInt32(comboBoxProduceUsageTo.Text);
DateTime RptParamsNominalToDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(toMonths, nextGenerateAndSendDate);
Run Code Online (Sandbox Code Playgroud)
..然后我想将"从"日期设置为最晚的时间,并将"到"设置为更接近时间.我第一次尝试这个:
DateTime RptParamsFromDate;
DateTime RptParamsToDate;
if (RptParamsNominalFromDate > RptParamsNominalToDate)
{
RptParamsFromDate = RptParamsNominalToDate;
RptParamsToDate = RptParamsNominalFromDate;
}
else if (RptParamsNominalToDate > RptParamsNominalFromDate)
{
RptParamsFromDate = RptParamsNominalFromDate;
RptParamsToDate = RptParamsNominalToDate;
}
Run Code Online (Sandbox Code Playgroud)
...但是失败了," 使用未分配的局部变量'RptParamsFromDate'"(和"RptParamsToDate "的错误相同).
所以我尝试给DateTimes一个值/非值,如下所示:
DateTime RptParamsFromDate = null;
DateTime RptParamsToDate = null;
Run Code Online (Sandbox Code Playgroud)
...但是这给了我," 无法将null转换为'System.DateTime',因为它是一个不可为空的值类型 "
所以我再次设置动作并尝试对DateTimes进行nullablizing:
DateTime? RptParamsFromDate = null;
DateTime? RptParamsToDate = null;
Run Code Online (Sandbox Code Playgroud)
...但后来我得到," 'System.Nullable'不包含'ToLongDateString'的定义,并且没有扩展方法'ToLongDateString'接受类型'System.Nullable'的第一个参数'(你是否错过了使用指令或汇编参考?) …