VS报错

2018-03-25  本文已影响18人  downdemo

此运算符的参数太多

class A {
    ...
    ostream& operator<<(ostream& os, A& a) { ... }
};
class A {
    ...
    friend ostream& operator<<(ostream& os, A& a) { ... }
};
ostream& operator<<(ostream& os, A& a) { ... }

error LNK2019: unresolved external symbol "public: __thiscall

~A();
~A() {}

error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'std::ofstream'

std::ofstream& operator<<(std::ofstream &of, A &a)
{
    of << a.ID << a.pwd;
    return of;
}
#include <fstream>
#include <string>

std::ofstream& operator<<(std::ofstream &of, A &a)
{
    of << a.ID << a.pwd;
    return of;
}
// 没有包含<string>
#include <iostream>
int main()
{
    std::string s("hello");
    std::cout << s;
}

// 没有包含<fstream>
#include <string>
int main()
{
    std::string s("hello");
    std::ofstream fout;
    fout.open("test.txt", std::ios_base::in);
    fout << s;
}

error LNK2001: unresolved external symbol

// var.h
// 声明一个全局变量
#ifndef VAR_H
#define VAR_H
#include <vector>
extern std::vector<int> v;
#endif

// func.h
// 声明函数
#ifndef FUNC_H
#define FUNC_H
void f();
#endif

// main.cpp
// 什么都不做
int main() {}

// func.cpp
// 给全局变量添加一个元素
#include "func.h"
#include "var.h"
void f()
{
    v.push_back(1);
}
// var.cpp
// 加上此文件即可
#include "var.h"
std::vector<int> v;
// 类A的声明在A.h中
// 类A的定义在A.cpp中

// var.h
#ifndef VAR_H
#define VAR_H

#include <string>
#include <map>
#include "A.h"

extern std::map<std::string, A> a;

#endif

// var.cpp
#include "var.h"
std::map<std::string, A> a;

// func.h
#ifndef FUNC_H
#define FUNC_H

void f();

#endif

// func.cpp
#include "func.h"
#include "var.h"

void f()
{
    A temp;
    a.insert(std::make_pair("hello", temp));
}

MFC报错

error C2065: 'IDD_DIALOG1' : undeclared identifier

error C2065: 'm_pSet' : undeclared identifier

error C2143: syntax error : missing ',' before '&'

// file B.h
void f(const A &p);

error C4430: missing type specifier

This function or variable may be unsafe

function XXX already has a body

上一篇下一篇

猜你喜欢

热点阅读