新手学c++遇到的问题 ,百度不到啊!
第一个问题asdf.h 头文件
extern const int fingers=10;
/*****在c++里面const 这个代表为是内部链接,为了让fingers有外部链接 所以,我用了 extern*******/
const char*warning="walk!";
1.cpp源文件
#include
#include"asdf.h"
int abc()
{
extern const int fingers;//声明!!
using namespace std;
extern int a;
cout<cout<return 0;
}
2.cpp 源文件2
#include
#include"asdf.h"
extern int a=10;
extern int abc();
int main()
{
extern const int fingers;//声明!!
using namespace std;
int a=12;
cout<abc();
cout<cin.get();
return 0;
}
/**************************然后******************************
Linking...
2.obj : error LNK2005: "char const * const warning" (?warning@@3PBDB) already defined in 1.obj
2.obj : error LNK2005: "int const fingers" (?fingers@@3HB) already defined in 1.obj
Debug/看看extern.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.***************************链接的时候错啦,什么鬼??****************************************/
第二个问题 new 的定位功能
#include
#include
const int B = 512;
const int N = 1;
char buffer[B];
int main()
{
using namespace std;
double *a;
double *b;
a = new double[20];
b = new (buffer)[N]; // vs2013 说我 这里错啦[应该要输入类型说明符
*b = 'A';
a[0] = 10.0;
int i;
for (i = 1; i<19; i++)
{
a[i] += a[i - 1];
}
cout << "address:" << a << endl;
cout << "b的地址是" << b << endl;
cout << "buffer的地址是:" << (void*)buffer;
delete[] a;
delete[] b;
return 0;
}
第三个问题
这个 问题 很神奇 我很喜欢!!
#include
#include
const int B=512;
int buffer[B];
int main()
{
using namespace std;
double *pd1;
double *pd2;
pd1=new double ;
pd2=new (buffer) double ;
*pd1=120.2;
*pd2=13.13;
cout<<*pd1<<"at"< cout<<*pd2<<"at"< cout<<"buffer"<<"at"< cin.get();
return 0;
}
********************** ********************
1,为什么 pd2是double的指针而,buffer 是int 的指针 他们在程序中显示的还是同样?
,2,当 char buffer 那么 要(void *)buffer 为什么不是 (double *)buffer?
******************* ************
第四个问题
*****************************************************************
#include
#include
#include
using namespace std;
const int S = 4;
const arraysnames =
{ "spring", "summer", "fall", "winter" };
void fill(array*pa);
void show(arrayda);
int main()
{
array expenses;
fill(&expenses);
show(expenses);//********说我没有标识show(expenses)。。*** 我明明标识了....
return 0;
}
void fill(array*pa)
{
for (int i = 0; i < S; i++)
{
cout << "enter " << snames[i] << "expenses";
cin >> (*pa)[i];
}
}
void show(arrayda)
{
double total = 0.0;
cout << "\nexpenses" << endl;
for (int i = 0; i < S; i++)
{
cout << snames[i] << ":$" << da[i] << endl;
total += da[i];
}
cout << "total expenses :$" << total << endl;
}
第五个问题 函数指针
**********************************************
#include
using namespace std;
void abc( int z ,int x, void*pf)(int&a,int&b));
void change(int&a ,int&b);
int main()
{
int x = 12;
int y = 123;
abc(x, y, change)
cout << y << endl;
cout << x << endl;
cin.get();
return 0;
}
void change(int &a, int&b)
{
int t;
t = a;
a = b;
b = t;
}
void abc(int z,int x, void(*pf)(int&a, int&b))
{
cout << ".....";
(*pf)(z,x)
}
****************************错了很多啊*******搞不懂***********
第六个问题 template 和 typename
********************还是懵逼啊*************运行环境vs2013***
#include
using namespace std;
template
void swap(T &a, T&b);
int main()
{
int i = 10;
int j = 20;
cout << "i,j=" << i << "," << j << endl;
cout << "using compier generated int sseapper " << endl;
swap(i,j);
cout << "now i,j=" << i << "," << j << endl;
double x = 24.5;
double y = 81.7;
cout << "x,y=" << x << "," << y << endl;
cout << "uisng ";
swap(x,y);
cout << "now ,x,y=" << x << "," << y<< endl;
cin.get();
cin.get();
return 0;
}
template
void swap(T&a, T&b)
{
T temp;
temp = a;
a = b;
b = temp;
}
同样的问题!!
#include
template
void swap(T &a, T &b);
template
void swap(T *a, T *b, int n);
void show(int a[]);
const int L = 8;
int main()
{
using namespace std;
int i = 10, j = 20;
cout << "i,j=" << i << "," << j << endl;
cout << "using " << endl;
swap(i, j);
cout << "i,j=" << i << "," << j << endl;
int d1[L] = { 0, 75, 7, 5, 75, 756, 762, 546 };
int d2[L] = { 1, 64, 64, 64, 74, 64, 3, 6, };
cout << "oringinal arrays" << endl;
show(d1);
show(d2);
cin.get();
return 0;
}
template
void swap(T &a, T&b)
{
T temp;
temp = a;
a = b;
b = temp;
}
template
void swap(T a[], T b[], int n)
{
T temp;
for (int i = 0; i < n; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void show(int a[])
{
using namespace std;
cout << a[0] << a[1] << endl;
cout << a[2] << a[3] << endl;
for (int i = 4; i < L; i++)
cout << a[i];
cout << endl;
}
说什么: 2 IntelliSense: 有多个 重载函数 "swap" 实例与参数列表匹配:
函数模板 "void swap(T &a, T &b)"
函数模板 "void std::swap(_Ty &, _Ty &)"
参数类型为: (int, int) c:\mycode\c++\c++ primer plus\函数探\代码\函数\函数\函数.cpp 306 2 函数
/**********说好的可以自己判断的啊!!我的重载没有冲突问题啊******/
错误 1 error C2668: “std::swap”: 对重载函数的调用不明确 c:\mycode\c++\c++ primer plus\函数探\代码\函数\函数\函数.cpp 306 1 函数
还是这个问题啊
我要疯了!!!,错误的提示一模一样!!,难道vs2013没有c++语言链接性了??
#include
template
void swap(T &a, T &b);
struct job
{
char name[40];
double salary;
int floor;
};
template <> void swap(job&j1, job&j2);
void show(job &j);
int main()
{
using namespace std;
cout.precision(2);
cout.setf(ios::fixed, ios::floatfield);
int i = 10;
int j = 20;
cout << "i,j=" << i << "," << j << endl;
job sue = { "susan yaffee", 73000.60, 7 };
job sidney = { "sidney taffee", 7860.73, 9 };
cout << "before job swapping" << endl;
show(sue);
show(sidney);
swap(sue, sidney);
cout << "after job swapping" << endl;
show(sue);
show(sidney);
cin.get();
return 0;
}
template <>void swap(job&j1, job&j2)
{
double t1;
int t2;
t1 = j1.salary;
j1.salary = j2.salary;
j2.salary = t1;
t2 = j1.floor;
j1.floor = j2.floor;
j2.floor = t2;
}
void show(job&j)
{
using namespace std;
cout << j.name << ":$" << j.salary
<< "on floor" << j.floor << endl;
}
*****************************************************************
第 七个问题 ..string 和 char * 的转换
#include
//#include
#include
int main()
{
using namespace std;
cout << "ssss" << endl;
string abc="sdfh";
int n = 10;
const char *p = abc.c_str();// 把const加上,cin.getline()有问题
cin.getline(p, n);//不加const char*p=abc.c_str()有问题
}
第八个问题 引用和string
#include
//#include"duixiang.h"
void getnamme( std:: string &a, int n)
{
using namespace std;
cout << "please input your name" << endl;
int i;
for (i = 0; i < n; i++)
cin >> a[i];
cout << a;/// **********
怎么就有错误了??a是一个string 类型的字符串引用!干嘛说我错!!
}
第九个问题 类 下面是一个头文件
#ifndef S
#define S
#include
class atm
{
private:
std::vector name(20);//说我(20)这里要插入类型说明符
public:
void getname(std::vector &a, int n);//说我std::vector缺少类模板的参数列表。。。为什么啊
};
#endif
*******************************************************
const Stock&topval(const Stock& S) const/*说我非成员函数上不可以用 const限定 后面的const明明 限定this指针的,不让它改变值的*/
{
if (S.total_val > total_val)
//说我S.要什么输入表达式。。。。
return s;
else
return *this;
}
***********************************