我试图在不使用IDE的情况下使用Visual Studio Express 2010 C++编译器.我在C:\ Program Files(x86)\ Microsoft Visual Studio 10.0\VC\bin中找到了cl.exe.但是我遇到了一些困难.首先,当我输入cl说"程序无法启动,因为您的计算机中缺少mspdb100.dll"时,它会弹出警告.
所以我将C:\ Program Files(x86)\ Microsoft Visual Studio 10.0\Common7\IDE添加到系统路径,然后再试一次,但这一次:
致命错误C1510:无法加载语言资源clui.dll.
任何想法如何解决这个,所以我可以编译?另外,我将如何设置路径,以便我可以在不包含cl.exe的解决方案文件夹中键入"cl main.cpp"等.目前我必须在bin文件夹中.谢谢.
c++ compiler-construction compiler-errors visual-studio-2010
我正在学习矢量并制作了一些代码来选择我可以在荷兰购买彩票的随机数字.但是虽然它运行,编译器警告我'从'time_t'转换为'unsigned int,可能会丢失数据'.
有人能发现造成这种情况的原因吗?我甚至没有在这段代码中定义任何unsigned int; 我理解默认情况下,int是一个signed int.感谢您的见解.
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
void print_numbers();
string print_color();
int main() {
srand(time(NULL));
print_numbers();
string color = print_color();
cout << color << endl;
system("PAUSE");
return 0;
}
//Fill vector with 6 random integers.
//
void print_numbers() {
vector<int> lucky_num;
for (int i = 0; i < 6; i++) {
lucky_num.push_back(1 + rand() % 45);
cout << lucky_num.at(i) << endl;
}
}
//Select random color from array.
//
string …Run Code Online (Sandbox Code Playgroud) 无法使用标头保护程序在vs 2010中编译任何代码.例如:
#ifndef SIMPLE.H
#define SIMPLE.H
#include <iostream>
class Place {
private:
int m_xplace;
int m_yplace;
Place(){}
public:
Place(int x, int y) : m_xplace(x), m_yplace(y) {}
void Move(int x, int y);
void set_place(int x, int y) {m_xplace = x, m_yplace = y;}
int get_place_x() {return m_xplace;}
int get_place_y() {return m_yplace;}
};
#endif
Run Code Online (Sandbox Code Playgroud)
我从编译器得到这个输出:
1>------ Build started: Project: 1, Configuration: Release Win32 ------
1>e:\projects\1\1\simple.h(1): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>e:\projects\1\1\simple.h(2): error C2008: '.' : unexpected …Run Code Online (Sandbox Code Playgroud) 我正在学习C#并编写了一些简单的代码,但我不明白为什么我必须在Main方法的范围内声明变量userChoice和numberR,而不是在类的范围内.如果我在类中声明它,我会得到构建错误
using System;
namespace FirstProgram
{
class Program
{
string userChoice;
int numbeR;
static void Main()
{
Console.WriteLine("Write a number...");
userChoice = Console.ReadLine();
numbeR = Convert.ToInt32(userChoice);
Console.WriteLine("You wrote {0}", numbeR);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但只有这样才能给我带来错误:
using System;
namespace FirstProgram
{
class Program
{
static void Main()
{
string userChoice;
int numbeR;
Console.WriteLine("Write a number...");
userChoice = Console.ReadLine();
numbeR = Convert.ToInt32(userChoice);
Console.WriteLine("You wrote {0}", numbeR);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不应该在Main中使用这两个变量,只需在上面的类中声明它们吗?我很困惑...感谢任何建议.
我已经安装了visual studio express 2010 c ++.然而,当试图跟随初学者书并制作hello world程序时,visual studio打开了一个项目:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,这看起来与我刚刚开始阅读的书中的c ++(C++ Without Fear)完全不同.事实上,如果我输入'cout',即使在输入使用命名空间std之后,也找不到cout.
这是书籍的例子:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "Never fear, C++ is here!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我刚开始学习C#.我正在读一本书,其中一个例子是这样的:
using System;
public class Example
{
public static void Main()
{
string myInput;
int myInt;
Console.Write("Please enter a number: ");
myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);
Console.WriteLine(myInt);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行并输入说'五'并点击返回时,我得到'输入字符串不正确格式'错误.我不明白的是,我将字符串myInput转换为数字不是我吗?微软称In32.Parse'将数字的字符串表示形式转换为32位有符号整数等价物.那么当我输入五个单词时怎么会不起作用呢?它应该转换为整数不应该...混淆.谢谢你的建议.
我不太确定c ++中的这个函数是如何工作的:
int rand_0toN1(int n) {
return rand() % n;
}
Run Code Online (Sandbox Code Playgroud)
互联网上的另一个教程说要在你需要做一些不同的范围之间得到一个随机数,但是在范围内是第一个数字,n是范围内的项数:
int number = a + rand( ) % n;
Run Code Online (Sandbox Code Playgroud)
我已阅读,它应该返回0,n-1和值之间的随机数,但它是如何做到这一点?我知道%表示除法并给出余数(因此5%2将是1)但是如何最终给出0到n-1之间的数字?感谢您帮助理解这一点.我想我不明白rand()函数返回什么.
在我正在阅读的书中学习基本的c ++,有这个例子:
#include <iostream>
using namespace std;
class Point {
private: // Data members (private)
int x, y;
public: // Member functions
void set(int new_x, int new_y);
int get_x();
int get_y();
};
void Point::set(int new_x, int new_y) {
x = new_x;
y = new_y;
}
int Point::get_x() {
return x;
}
int Point::get_y() {
return y;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在c ++中是否不可能在类本身中包含成员函数的定义?以上看起来相当混乱.书中说要定义一个类成员函数,你应该使用'return_type class_name :: function(){arguments}.但是在C#中你可以在同一个类中完成它而且代码更少.我在c ++中找不到很多关于属性的东西.感谢帮助.
刚开始学习c ++中的算法.我的书提到了数字标题中定义的简单只读算法.它需要三个参数,第一个是指定一个或多个要求的元素的迭代器,第三个是求和的初始值.但是当我尝试这样做时:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
vector<int> numbers;
//Fill vector with numbers.
for (vector<int>::size_type i = 0; i != 10; i++) {
numbers.push_back(i);
}
int sum = accumulate(numbers.begin(), numbers.end(), 1);
cout << numbers.size() << endl;
cout << sum << endl;
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的输出为:10,46.10加1怎么能是46?我不能在这里看到我的错误,感谢您的洞察力.
我正在摆弄VS2010中的性能向导,即测试仪器(函数调用次数和时序).
在了解了C++ STL中的向量之后,我决定看看有关填充100万个整数向量的性能的信息:
#include <iostream>
#include <vector>
void generate_ints();
int main() {
generate_ints();
return 0;
}
void generate_ints() {
typedef std::vector<int> Generator;
typedef std::vector<int>::iterator iter;
typedef std::vector<int>::size_type size;
Generator generator;
for (size i = 0; i != 1000000; ++i) {
generator.push_back(i);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的是:上述2402.37毫秒的经过时间.但我了解到,当向量耗尽时,向量必须调整自身大小,因为它们在内存中是连续的.因此,我认为通过对上述内容进行一次添加可以获得更好的性能:
generate.reserve(1000000);
Run Code Online (Sandbox Code Playgroud)
然而,这使程序的执行时间加倍到大约5000毫秒.这是函数调用的屏幕截图,左侧没有上面的代码行,右侧有.我真的不明白这个结果,如果你知道如果用一个吨来填充它,我学到了如何定义矢量容量对我来说没有意义是一件好事.指定保留基本上使大多数函数调用加倍.
我不明白为什么我得到运行时错误:
变量ia正在使用而未被初始化.
但是,据我所知,我已初步化了它.
#include <iostream>
using namespace std;
int main()
{
//array dimensions
const int row_size = 2;
const int col_size = 4;
//array definition
int ia[row_size][col_size] = {{0, 1, 2, 3},{5, 6, 7, 8}};
cout << ia[2][4];
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)